Menu

Testing JavaScript performance

May 3, 2017 - javascript
/*
 https://benchmarkjs.com/

INITIALIZE
==============
sudo npm i -g npm       // update npm
sudo npm update -g      // update packages
npm init                // initiate project

INSTALATION
==============
sudo npm i --save benchmark
sudo npm i --save lodash
sudo npm i --save platform

RUN TEST
===================
node test.js

RESULT
===================
RegExp#test x 12,930,284 ops/sec ±2.44% (62 runs sampled)
String#indexOf x 20,757,535 ops/sec ±2.82% (62 runs sampled)
String#match x 7,125,209 ops/sec ±8.82% (54 runs sampled)
Fastest is String#indexOf
 */

var _ = require('lodash');
var platform = require('platform');
var Benchmark = require('benchmark');

var suite = new Benchmark.Suite;

// add tests
suite.add('RegExp#test', function () {
        /o/.test('Hello World!');
    })
    .add('String#indexOf', function () {
        'Hello World!'.indexOf('o') > -1;
    })
    .add('String#match', function () {
        !!'Hello World!'.match(/o/);
    })
    // add listeners
    .on('cycle', function (event) {
        console.log(String(event.target));
    })
    .on('complete', function () {
        console.log('Fastest is ' + this.filter('fastest')
            .map('name'));
    })
    // run async
    .run({
        'async': true
    });