Skip to content

Commit

Permalink
production commit
Browse files Browse the repository at this point in the history
  • Loading branch information
InteractiveTimmy committed Dec 12, 2018
1 parent 746c38c commit d78931b
Show file tree
Hide file tree
Showing 15 changed files with 446 additions and 14 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# js-spt

## Description

JS-SPT is a javascript Structure Performance Test designed to validate which data structure takes the least amount of time to handle a set of operations. This project was designed to help guide other projects and their data structures by demonstrating real world performance differences between each data structuring method in live loops.

## Installation

JS-SPT is prebuilt, and contains a webpage example as well as a NodeJS example. If you would like to rebiuld the bundled files, you will need to install the dev-dependencies via npm using the command `npm install`.

## Usage

### Building

To build the project, simply run the following command: `npm run build`. This will build a **spt.js** *UMD* file as well as a **spt.module.js** *ES6* file.

If you would like to actively modify and rebuild on `/src/**` change, run the following command: `npm run watch`. This will build the same two files mentioned above.

### Examples

To run the NodeJS example, simply run the command `npm start` from the projects root directory. This will run the application using the default controlled variables:
* 50 Trials
* 1000000 Operations per Trial
* An Array, Object, and Class Structure

To run the Web Example, navigate via your file explorer to [/examples/](https://github.com/InteractiveTimmy/js-spt/tree/master/examples) and load the [./index.html](https://github.com/InteractiveTimmy/js-spt/blob/master/examples/index.html) file into your favorite browser. The example will contain a form for customizing the controls for the test as well as some instructional information in regards to usage.
108 changes: 102 additions & 6 deletions build/spt.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,107 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.ECS = factory());
}(this, (function () { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.SPT = {})));
}(this, (function (exports) { 'use strict';

var spt = { };
class StructClass
{
constructor ( )
{
this.data = new Float32Array( [
Math.random( ),
Math.random( ),
Math.random( )
] );
}

return spt;
operate ( )
{
this.data[0] = Math.random( ),
this.data[1] = Math.random( ),
this.data[2] = Math.random( );
}
}

const StructObject = {
create: ( ) => {
return {
data: new Float32Array( [
Math.random( ),
Math.random( ),
Math.random( )
] )
};
},
operate: ( struct ) => {
struct.data[0] = Math.random( );
struct.data[1] = Math.random( );
struct.data[2] = Math.random( );
}
};

const StructArray = {
create: ( ) => {
return new Float32Array( [
Math.random( ),
Math.random( ),
Math.random( )
] );
},
operate: ( struct ) => {
struct[0] = Math.random( );
struct[1] = Math.random( );
struct[2] = Math.random( );
}
};

class Trial
{
constructor ( name, struct, type )
{
this.name = name;
this.operate = ( ) => { console.log( 'hello' ); };

this.begin = null;
this.end = null;
this.delta = null;

this.data = [ ];

this.mean = 0;

if ( type === 'class' )
{
this.struct = new struct( );
this.operate = ( ) => { this.struct.operate( ); };
}
else if ( type === 'object' )
{
this.struct = struct.create( );
this.operate = ( ) => { struct.operate( this.struct ); };
}
else if ( type === 'array' )
{
this.struct = struct.create( );
this.operate = ( ) => { struct.operate( this.struct ); };
}
}
}

class Target
{
constructor ( )
{

}
}

exports.StructClass = StructClass;
exports.StructObject = StructObject;
exports.StructArray = StructArray;
exports.Trial = Trial;
exports.Target = Target;

Object.defineProperty(exports, '__esModule', { value: true });

})));
94 changes: 92 additions & 2 deletions build/spt.module.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,93 @@
var spt = { };
class StructClass
{
constructor ( )
{
this.data = new Float32Array( [
Math.random( ),
Math.random( ),
Math.random( )
] );
}

export default spt;
operate ( )
{
this.data[0] = Math.random( ),
this.data[1] = Math.random( ),
this.data[2] = Math.random( );
}
}

const StructObject = {
create: ( ) => {
return {
data: new Float32Array( [
Math.random( ),
Math.random( ),
Math.random( )
] )
};
},
operate: ( struct ) => {
struct.data[0] = Math.random( );
struct.data[1] = Math.random( );
struct.data[2] = Math.random( );
}
};

const StructArray = {
create: ( ) => {
return new Float32Array( [
Math.random( ),
Math.random( ),
Math.random( )
] );
},
operate: ( struct ) => {
struct[0] = Math.random( );
struct[1] = Math.random( );
struct[2] = Math.random( );
}
};

class Trial
{
constructor ( name, struct, type )
{
this.name = name;
this.operate = ( ) => { console.log( 'hello' ); };

this.begin = null;
this.end = null;
this.delta = null;

this.data = [ ];

this.mean = 0;

if ( type === 'class' )
{
this.struct = new struct( );
this.operate = ( ) => { this.struct.operate( ); };
}
else if ( type === 'object' )
{
this.struct = struct.create( );
this.operate = ( ) => { struct.operate( this.struct ); };
}
else if ( type === 'array' )
{
this.struct = struct.create( );
this.operate = ( ) => { struct.operate( this.struct ); };
}
}
}

class Target
{
constructor ( )
{

}
}

export { StructClass, StructObject, StructArray, Trial, Target };
58 changes: 56 additions & 2 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,63 @@
<html>
<head>
<meta charset="UTF-8">
<title>title</title>
<title>JavaScript Structure Performance Test</title>
<script type='text/javascript' charset='UTF-8' src="../build/spt.js"></script>
<script type='text/javascript' charset='UTF-8' src="index.js"></script>
</head>
<body>
<h1>TODO</h1>
<h1>JavaScript Structure Performance Test</h1>
<p>JavaScript Structure Performance Test is designed to validate which data structure takes the least amount of time to handle a set of operations. Each data structure, upon construction, creates a Float32Array of three random 32-bit floating point numbers between 0 and 1. Each data structure also contains an operate( ) method which mutates the data by assigning new random values to each of the three Float32Array values.</p>
<ul>
<li>The Javascript Array uses a Create( ) method to generate and return a simple Float32Array</li>
<li>The Javascript Object uses a Create( ) method to generate and return an object with the structure { data: Float32Array }</li>
<li>The Javascript Class uses a standard Class.constructor( ) method to return a newly generated JavaScript Class Object with the structure { data: Float32Array }</li>
</ul>
<p>Note: This application can also be ran via NodeJS / NPM from the project's root directory. See README.md for more information</p>
<h3>Testing Form</h3>
<form>
# of Trials: <input id="elm-trials" type="number" name="trials" min="1" max="1000" value="20"><br /><br />
# of Operations: <input id="elm-operations" type="number" name="operations" min="1" max="1000000000" value="1000000"><br /><br />

Structures to Test: <br />
<input id="elm-array" type="checkbox" name="StructArray" value="array"> Array<br />
<input id="elm-object" type="checkbox" name="StructObject" value="object"> Object<br />
<input id="elm-class" type="checkbox" name="StructClass" value="class"> Class<br />
<br />
<button type="button" value="start" onclick="handleStart( )">Start</button>
</form>

<div id="output"></div>

<script type='text/javascript' charset='UTF-8'>
function handleStart( ) {
trials.length = 0;

let elmOutput = document.getElementById( 'output' );

while ( elmOutput.firstChild )
{ elmOutput.removeChild( elmOutput.firstChild ); }

controls.operations = document.getElementById( 'elm-operations' ).value;
controls.trials = document.getElementById( 'elm-trials' ).value;

if ( document.getElementById( 'elm-array' ).checked )
{ trials.push( new SPT.Trial( 'array', SPT.StructArray, 'array' ) ); }
if ( document.getElementById( 'elm-object' ).checked )
{ trials.push( new SPT.Trial( 'object', SPT.StructObject, 'object' ) ); }
if ( document.getElementById( 'elm-class' ).checked )
{ trials.push( new SPT.Trial( 'class', SPT.StructClass, 'class' ) ); }

start( );

let elm;

trials.forEach( trial => {
elm = document.createElement( 'p' );
elm.innerHTML = `${trial.name} had an average trial delta of ${trial.mean}ms`;
elmOutput.appendChild( elm );
} );
}
</script>
</body>
</html>
65 changes: 64 additions & 1 deletion examples/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,64 @@
// TODO
'use strict';

let SPT;
let instanceType;
let target;

try
{
SPT = window.SPT;
instanceType = 'browser';
}
catch
{
SPT = require( '../build/spt.js' );
instanceType = 'node'
}

console.log( `${instanceType} instance detected` );

let controls = { };
let trials = [ ];

function init ( )
{
controls.operations = 1000000;
controls.trials = 50;

trials.push( ...[
new SPT.Trial( 'class', SPT.StructClass, 'class' ),
new SPT.Trial( 'object', SPT.StructObject, 'object' ),
new SPT.Trial( 'array', SPT.StructArray, 'array' )
] );

start( );
}

function start ( )
{
trials.forEach( trial => {
for ( let x = 0; x < controls.trials; x++ )
{
trial.begin = Date.now( );

for ( let y = 0; y < controls.operations; y++ )
{ trial.operate( ); }

trial.end = Date.now( );
trial.delta = trial.end - trial.begin;

trial.data.push( trial.delta );
}

trial.data.forEach( item => {
trial.mean += item;
} );

trial.mean /= trial.data.length;

console.log( `${trial.name} had an average trial delta of ${trial.mean}ms` );
} );
}

if ( instanceType === 'node' )
{ init( ); }
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-spt",
"version": "0.1.0",
"version": "1.0.0",
"description": "JavaScript Structure Performance Test",
"main": "index.js",
"directories": {
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export default {
output: [
{
format: 'umd',
name: 'ECS',
name: 'SPT',
file: 'build/spt.js',
indent: '\t'
},
Expand Down
2 changes: 2 additions & 0 deletions src/objects/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as Trial } from './trial.js';
export { default as Target } from './target.js';
Loading

0 comments on commit d78931b

Please sign in to comment.