UNPKG

2.78 kBJavaScriptView Raw
1var fixtures = [
2 {
3 description: 'strings',
4 args: ['one', 'two', 'three'],
5 expected: 'one two three'
6 },
7 {
8 description: 'object',
9 args: [{ one: true, two: true, three: false }],
10 expected: 'one two'
11 },
12 {
13 description: 'strings, object',
14 args: ['one', 'two', { four: true, three: false }],
15 expected: 'one two four'
16 },
17 {
18 description: 'mix',
19 args: ['one', { two: true, three: false }, { four: 'four', five: true }, 6, {}],
20 expected: 'one two four five 6'
21 },
22 {
23 description: 'arrays',
24 args: [['one', 'two'], ['three'], ['four', ['five']], [{ six: true }, { seven: false }]],
25 expected: 'one two three four five six'
26 }
27];
28
29var local = require('../');
30var dedupe = require('../dedupe');
31var localPackage = require('../package.json');
32
33try {
34 var npm = require('classnames');
35 var npmDedupe = require('classnames/dedupe');
36 var npmPackage = require('./node_modules/classnames/package.json');
37} catch (e) {
38 console.log('There was an error loading the benchmark classnames package.\n' +
39 'Please make sure you have run `npm install` in ./benchmarks\n');
40 process.exit(0);
41}
42
43if (localPackage.version !== npmPackage.version) {
44 console.log('Your local version (' + localPackage.version + ') does not match the installed version (' + npmPackage.version + ')\n\n' +
45 'Please run `npm update` in ./benchmarks to ensure you are benchmarking\n' +
46 'the latest version of this package.\n');
47 process.exit(0);
48}
49
50var assert = require('assert');
51var benchmark = require('benchmark');
52
53function sortClasses (str) {
54 return str.split(' ').sort().join(' ');
55}
56
57fixtures.forEach(function (f) {
58 // sort assertions because dedupe returns results in a different order
59 assert.equal(sortClasses(local.apply(null, f.args)), sortClasses(f.expected));
60 assert.equal(sortClasses(dedupe.apply(null, f.args)), sortClasses(f.expected));
61 assert.equal(sortClasses(npm.apply(null, f.args)), sortClasses(f.expected));
62 assert.equal(sortClasses(npmDedupe.apply(null, f.args)), sortClasses(f.expected));
63
64 var suite = new benchmark.Suite();
65
66 suite.add('local#' + f.description, function () {
67 local.apply(null, f.args);
68 });
69
70 suite.add(' npm#' + f.description, function () {
71 npm.apply(null, f.args);
72 });
73
74 suite.add('local/dedupe#' + f.description, function () {
75 dedupe.apply(null, f.args);
76 });
77
78 suite.add(' npm/dedupe#' + f.description, function () {
79 npmDedupe.apply(null, f.args);
80 });
81
82 // after each cycle
83 suite.on('cycle', function (event) {
84 console.log('*', String(event.target));
85 });
86
87 // other handling
88 suite.on('complete', function () {
89 console.log('\n> Fastest is' + (' ' + this.filter('fastest').pluck('name').join(' | ')).replace(/\s+/, ' ') + '\n');
90 });
91
92 suite.on('error', function (event) {
93 throw event.target.error;
94 });
95
96 suite.run();
97});