From d78931b8c0a97ee7e4138aa093bd3100a8481e3b Mon Sep 17 00:00:00 2001 From: InteractiveTimmy Date: Wed, 12 Dec 2018 15:30:49 -0500 Subject: [PATCH] production commit --- README.md | 26 ++++++++ build/spt.js | 108 ++++++++++++++++++++++++++++++-- build/spt.module.js | 94 ++++++++++++++++++++++++++- examples/index.html | 58 ++++++++++++++++- examples/index.js | 65 ++++++++++++++++++- package.json | 2 +- rollup.config.js | 2 +- src/objects/index.js | 2 + src/objects/target.js | 9 +++ src/objects/trial.js | 34 ++++++++++ src/spt.js | 3 +- src/structures/index.js | 3 + src/structures/struct-array.js | 16 +++++ src/structures/struct-class.js | 20 ++++++ src/structures/struct-object.js | 18 ++++++ 15 files changed, 446 insertions(+), 14 deletions(-) create mode 100644 src/objects/index.js create mode 100644 src/objects/target.js create mode 100644 src/objects/trial.js create mode 100644 src/structures/index.js create mode 100644 src/structures/struct-array.js diff --git a/README.md b/README.md index e69de29..a3db9ed 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file diff --git a/build/spt.js b/build/spt.js index beba321..b29f16b 100644 --- a/build/spt.js +++ b/build/spt.js @@ -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 }); }))); diff --git a/build/spt.module.js b/build/spt.module.js index 4315ef5..58c387f 100644 --- a/build/spt.module.js +++ b/build/spt.module.js @@ -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 }; diff --git a/examples/index.html b/examples/index.html index 7ac1c73..7fe1b36 100644 --- a/examples/index.html +++ b/examples/index.html @@ -2,9 +2,63 @@ - title + JavaScript Structure Performance Test + + -

TODO

+

JavaScript Structure Performance Test

+

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.

+ +

Note: This application can also be ran via NodeJS / NPM from the project's root directory. See README.md for more information

+

Testing Form

+
+ # of Trials:

+ # of Operations:

+ + Structures to Test:
+ Array
+ Object
+ Class
+
+ +
+ +
+ + \ No newline at end of file diff --git a/examples/index.js b/examples/index.js index 0ffdd02..5584fa0 100644 --- a/examples/index.js +++ b/examples/index.js @@ -1 +1,64 @@ -// TODO \ No newline at end of file +'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( ); } \ No newline at end of file diff --git a/package.json b/package.json index fd8b968..adc69c3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "js-spt", - "version": "0.1.0", + "version": "1.0.0", "description": "JavaScript Structure Performance Test", "main": "index.js", "directories": { diff --git a/rollup.config.js b/rollup.config.js index 0caded4..8084017 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -3,7 +3,7 @@ export default { output: [ { format: 'umd', - name: 'ECS', + name: 'SPT', file: 'build/spt.js', indent: '\t' }, diff --git a/src/objects/index.js b/src/objects/index.js new file mode 100644 index 0000000..a13cec6 --- /dev/null +++ b/src/objects/index.js @@ -0,0 +1,2 @@ +export { default as Trial } from './trial.js'; +export { default as Target } from './target.js'; \ No newline at end of file diff --git a/src/objects/target.js b/src/objects/target.js new file mode 100644 index 0000000..af5cf8d --- /dev/null +++ b/src/objects/target.js @@ -0,0 +1,9 @@ +class Target +{ + constructor ( ) + { + + } +} + +export default Target; \ No newline at end of file diff --git a/src/objects/trial.js b/src/objects/trial.js new file mode 100644 index 0000000..39b4f9e --- /dev/null +++ b/src/objects/trial.js @@ -0,0 +1,34 @@ +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 ); } + } + } +} + +export default Trial; \ No newline at end of file diff --git a/src/spt.js b/src/spt.js index 3caefef..4e6f358 100644 --- a/src/spt.js +++ b/src/spt.js @@ -1,3 +1,4 @@ 'use strict'; -export default { }; \ No newline at end of file +export * from './structures/index.js'; +export * from './objects/index.js'; \ No newline at end of file diff --git a/src/structures/index.js b/src/structures/index.js new file mode 100644 index 0000000..ee54dc8 --- /dev/null +++ b/src/structures/index.js @@ -0,0 +1,3 @@ +export { default as StructClass } from './struct-class.js'; +export { default as StructObject } from './struct-object.js'; +export { default as StructArray } from './struct-array.js'; \ No newline at end of file diff --git a/src/structures/struct-array.js b/src/structures/struct-array.js new file mode 100644 index 0000000..e4fa9c5 --- /dev/null +++ b/src/structures/struct-array.js @@ -0,0 +1,16 @@ +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( ); + } +} + +export default StructArray; \ No newline at end of file diff --git a/src/structures/struct-class.js b/src/structures/struct-class.js index e69de29..fa86233 100644 --- a/src/structures/struct-class.js +++ b/src/structures/struct-class.js @@ -0,0 +1,20 @@ +class StructClass +{ + constructor ( ) + { + this.data = new Float32Array( [ + Math.random( ), + Math.random( ), + Math.random( ) + ] ); + } + + operate ( ) + { + this.data[0] = Math.random( ), + this.data[1] = Math.random( ), + this.data[2] = Math.random( ) + } +} + +export default StructClass; \ No newline at end of file diff --git a/src/structures/struct-object.js b/src/structures/struct-object.js index e69de29..805f535 100644 --- a/src/structures/struct-object.js +++ b/src/structures/struct-object.js @@ -0,0 +1,18 @@ +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( ); + } +} + +export default StructObject; \ No newline at end of file