Skip to content

Commit

Permalink
Merge pull request #100 from victor-homyakov/benchmarks-browser
Browse files Browse the repository at this point in the history
Added benchmarks for browsers
  • Loading branch information
dcousens committed Mar 27, 2017
2 parents 51b3524 + 0099117 commit 0469d5f
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 76 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -24,3 +24,5 @@ node_modules

# Mac OS X DS_Store
.DS_Store

benchmarks/runInBrowser.bundle.js
12 changes: 12 additions & 0 deletions benchmarks/benchmarks.html
@@ -0,0 +1,12 @@
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>Benchmarks</title>
</head>
<body>
<div id="results"></div>
<div id="loader">Wait please&hellip;</div>
</body>
</html>
<script src="runInBrowser.bundle.js"></script>
29 changes: 29 additions & 0 deletions benchmarks/fixtures.js
@@ -0,0 +1,29 @@
var fixtures = [
{
description: 'strings',
args: ['one', 'two', 'three'],
expected: 'one two three'
},
{
description: 'object',
args: [{one: true, two: true, three: false}],
expected: 'one two'
},
{
description: 'strings, object',
args: ['one', 'two', {four: true, three: false}],
expected: 'one two four'
},
{
description: 'mix',
args: ['one', {two: true, three: false}, {four: 'four', five: true}, 6, {}],
expected: 'one two four five 6'
},
{
description: 'arrays',
args: [['one', 'two'], ['three'], ['four', ['five']], [{six: true}, {seven: false}]],
expected: 'one two three four five six'
}
];

module.exports = fixtures;
86 changes: 11 additions & 75 deletions benchmarks/run.js
@@ -1,97 +1,33 @@
var fixtures = [
{
description: 'strings',
args: ['one', 'two', 'three'],
expected: 'one two three'
},
{
description: 'object',
args: [{ one: true, two: true, three: false }],
expected: 'one two'
},
{
description: 'strings, object',
args: ['one', 'two', { four: true, three: false }],
expected: 'one two four'
},
{
description: 'mix',
args: ['one', { two: true, three: false }, { four: 'four', five: true }, 6, {}],
expected: 'one two four five 6'
},
{
description: 'arrays',
args: [['one', 'two'], ['three'], ['four', ['five']], [{ six: true }, { seven: false }]],
expected: 'one two three four five six'
}
];

var fixtures = require('./fixtures');
var local = require('../');
var dedupe = require('../dedupe');
var localPackage = require('../package.json');

function log (message) {
console.log(message);
}

try {
var npm = require('classnames');
var npmDedupe = require('classnames/dedupe');
var npmPackage = require('./node_modules/classnames/package.json');
} catch (e) {
console.log('There was an error loading the benchmark classnames package.\n' +
log('There was an error loading the benchmark classnames package.\n' +
'Please make sure you have run `npm install` in ./benchmarks\n');
process.exit(0);
}

if (localPackage.version !== npmPackage.version) {
console.log('Your local version (' + localPackage.version + ') does not match the installed version (' + npmPackage.version + ')\n\n' +
log('Your local version (' + localPackage.version + ') does not match the installed version (' + npmPackage.version + ')\n\n' +
'Please run `npm update` in ./benchmarks to ensure you are benchmarking\n' +
'the latest version of this package.\n');
process.exit(0);
}

var assert = require('assert');
var benchmark = require('benchmark');

function sortClasses (str) {
return str.split(' ').sort().join(' ');
}
var runChecks = require('./runChecks');
var runSuite = require('./runSuite');

fixtures.forEach(function (f) {
// sort assertions because dedupe returns results in a different order
assert.equal(sortClasses(local.apply(null, f.args)), sortClasses(f.expected));
assert.equal(sortClasses(dedupe.apply(null, f.args)), sortClasses(f.expected));
assert.equal(sortClasses(npm.apply(null, f.args)), sortClasses(f.expected));
assert.equal(sortClasses(npmDedupe.apply(null, f.args)), sortClasses(f.expected));

var suite = new benchmark.Suite();

suite.add('local#' + f.description, function () {
local.apply(null, f.args);
});

suite.add(' npm#' + f.description, function () {
npm.apply(null, f.args);
});

suite.add('local/dedupe#' + f.description, function () {
dedupe.apply(null, f.args);
});

suite.add(' npm/dedupe#' + f.description, function () {
npmDedupe.apply(null, f.args);
});

// after each cycle
suite.on('cycle', function (event) {
console.log('*', String(event.target));
});

// other handling
suite.on('complete', function () {
console.log('\n> Fastest is' + (' ' + this.filter('fastest').pluck('name').join(' | ')).replace(/\s+/, ' ') + '\n');
});

suite.on('error', function (event) {
throw event.target.error;
});

suite.run();
runChecks(local, npm, dedupe, npmDedupe, f);
runSuite(local, npm, dedupe, npmDedupe, f, log);
});
15 changes: 15 additions & 0 deletions benchmarks/runChecks.js
@@ -0,0 +1,15 @@
var assert = require('assert');

function sortClasses (str) {
return str.split(' ').sort().join(' ');
}

function runChecks (local, npm, dedupe, npmDedupe, fixture) {
// sort assertions because dedupe returns results in a different order
assert.equal(sortClasses(local.apply(null, fixture.args)), sortClasses(fixture.expected));
assert.equal(sortClasses(dedupe.apply(null, fixture.args)), sortClasses(fixture.expected));
assert.equal(sortClasses(npm.apply(null, fixture.args)), sortClasses(fixture.expected));
assert.equal(sortClasses(npmDedupe.apply(null, fixture.args)), sortClasses(fixture.expected));
}

module.exports = runChecks;
50 changes: 50 additions & 0 deletions benchmarks/runInBrowser.js
@@ -0,0 +1,50 @@
var fixtures = require('./fixtures');
var local = require('../');
var dedupe = require('../dedupe');
var localPackage = require('../package.json');

var npm = require('classnames');
var npmDedupe = require('classnames/dedupe');
var npmPackage = require('./node_modules/classnames/package.json');

function log (message) {
console.log(message);
var results = document.getElementById('results');
//noinspection InnerHTMLJS
results.innerHTML += (message + '\n').replace(/\n/g, '<br/>');
}

if (localPackage.version !== npmPackage.version) {
log('Your local version (' + localPackage.version + ') does not match the installed version (' + npmPackage.version + ')\n\n' +
'Please run `npm update` in ./benchmarks to ensure you are benchmarking\n' +
'the latest version of this package.\n');
return;
}

function iterate (array, iterator, i, callback) {
if (i >= 0 && i < array.length) {
iterator(array[i], i, array);
setTimeout(iterate.bind(null, array, iterator, i + 1, callback), 1);
} else if (callback) {
callback();
}
}

function deferredForEach (array, iterator, callback) {
iterate(array, iterator, 0, callback);
}

var runSuite = require('./runSuite');

window.onload = function () {
//noinspection PlatformDetectionJS
log(navigator.userAgent);
setTimeout(function () {
deferredForEach(fixtures, function (f) {
runSuite(local, npm, dedupe, npmDedupe, f, log);
}, function () {
log('Finished');
document.getElementById('loader').style.display = 'none';
});
}, 100);
};
40 changes: 40 additions & 0 deletions benchmarks/runSuite.js
@@ -0,0 +1,40 @@
var benchmark = require('benchmark');

function runSuite (local, npm, dedupe, npmDedupe, fixture, log) {
var suite = new benchmark.Suite();

suite.add('local#' + fixture.description, function () {
local.apply(null, fixture.args);
});

suite.add(' npm#' + fixture.description, function () {
npm.apply(null, fixture.args);
});

suite.add('local/dedupe#' + fixture.description, function () {
dedupe.apply(null, fixture.args);
});

suite.add(' npm/dedupe#' + fixture.description, function () {
npmDedupe.apply(null, fixture.args);
});

// after each cycle
suite.on('cycle', function (event) {
log('* ' + String(event.target));
});

// other handling
suite.on('complete', function () {
log('\n> Fastest is' + (' ' + this.filter('fastest').pluck('name').join(' | ')).replace(/\s+/, ' ') + '\n');
});

suite.on('error', function (event) {
log(event.target.error.message);
throw event.target.error;
});

suite.run();
}

module.exports = runSuite;
6 changes: 5 additions & 1 deletion package.json
Expand Up @@ -11,6 +11,8 @@
},
"scripts": {
"benchmarks": "node ./benchmarks/run",
"benchmarks-browserify": "./node_modules/.bin/browserify ./benchmarks/runInBrowser.js >./benchmarks/runInBrowser.bundle.js",
"benchmarks-in-browser": "./node_modules/.bin/opn ./benchmarks/benchmarks.html",
"test": "mocha tests/*.js"
},
"keywords": [
Expand All @@ -24,6 +26,8 @@
],
"devDependencies": {
"benchmark": "^1.0.0",
"mocha": "^2.1.0"
"browserify": "^14.1.0",
"mocha": "^2.1.0",
"opn-cli": "^3.1.0"
}
}

0 comments on commit 0469d5f

Please sign in to comment.