Skip to content

Instantly share code, notes, and snippets.

@Tom32i
Last active October 13, 2015 09:57
Show Gist options
  • Select an option

  • Save Tom32i/821cbd861ba2beafd0f6 to your computer and use it in GitHub Desktop.

Select an option

Save Tom32i/821cbd861ba2beafd0f6 to your computer and use it in GitHub Desktop.

Revisions

  1. Tom32i revised this gist Oct 13, 2015. 1 changed file with 12 additions and 5 deletions.
    17 changes: 12 additions & 5 deletions benchmark.js
    Original file line number Diff line number Diff line change
    @@ -2,24 +2,31 @@ if (typeof(require) !== 'undefined') {
    var Benchmark = require('benchmark');
    }

    var suite = new Benchmark.Suite;
    var suite = new Benchmark.Suite('My benchmark');

    // Functions to compare
    suite
    .add('function#A', function() {
    // Version A
    })
    .add('function#B', function() {
    // Version B
    })
    // add listeners

    // Listeners
    .on('start', function() {
    console.log('Started');
    console.log('Started "%s"', this.name);

    if (typeof(document) !== 'undefined') {
    document.title = this.name;
    }
    })
    .on('cycle', function(event) {
    console.log(String(event.target));
    })
    .on('complete', function() {
    console.log('Fastest is ' + this.filter('fastest').pluck('name'));
    })
    // run async
    .run({ 'async': true });

    // Run
    .run(/*{ 'async': true }*/);
  2. Tom32i revised this gist Oct 13, 2015. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions benchmark.js
    Original file line number Diff line number Diff line change
    @@ -6,11 +6,10 @@ var suite = new Benchmark.Suite;

    suite
    .add('function#A', function() {
    return Math.sqrt(Math.pow(point.x1 - point.x2, 2) + Math.pow(point.y1 - point.y2, 2));
    // Version A
    })
    .add('function#B', function() {
    var x = point.x1 - point.x2, y = point.y1 - point.y2;
    return Math.sqrt(x*x + y*y);
    // Version B
    })
    // add listeners
    .on('start', function() {
  3. Tom32i revised this gist Oct 13, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,6 @@
    </head>
    <body>
    <script src="node_modules/benchmark/benchmark.js"></script>
    <script src="index.js"></script>
    <script src="benchmark.js"></script>
    </body>
    </html>
  4. Tom32i revised this gist Oct 13, 2015. 3 changed files with 27 additions and 1 deletion.
    14 changes: 14 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    # Benchmark in node.js & browser

    ## Install
    npm install

    ## Usage:

    ### Node:

    node benchmark.js

    ### Browser:

    Navigate to index.html and open console.
    2 changes: 1 addition & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    <title>Benchmark</title>
    </head>
    <body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
    <script src="node_modules/benchmark/benchmark.js"></script>
    <script src="index.js"></script>
    </body>
    </html>
    12 changes: 12 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    {
    "name": "benchmark",
    "version": "1.0.0",
    "description": "A node.js + browser compatible benchmark suite",
    "main": "benchmark.js",
    "author": "Thomas Jarrand",
    "license": "MIT",
    "dependencies": {
    "benchmark": "^1.0.0",
    "microtime": "^2.0.0"
    }
    }
  5. Tom32i created this gist Oct 13, 2015.
    26 changes: 26 additions & 0 deletions benchmark.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    if (typeof(require) !== 'undefined') {
    var Benchmark = require('benchmark');
    }

    var suite = new Benchmark.Suite;

    suite
    .add('function#A', function() {
    return Math.sqrt(Math.pow(point.x1 - point.x2, 2) + Math.pow(point.y1 - point.y2, 2));
    })
    .add('function#B', function() {
    var x = point.x1 - point.x2, y = point.y1 - point.y2;
    return Math.sqrt(x*x + y*y);
    })
    // add listeners
    .on('start', function() {
    console.log('Started');
    })
    .on('cycle', function(event) {
    console.log(String(event.target));
    })
    .on('complete', function() {
    console.log('Fastest is ' + this.filter('fastest').pluck('name'));
    })
    // run async
    .run({ 'async': true });
    10 changes: 10 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    <!DOCTYPE html>
    <html>
    <head>
    <title>Benchmark</title>
    </head>
    <body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
    <script src="index.js"></script>
    </body>
    </html>