Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benchmark combo #189

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .npmignore
Expand Up @@ -2,3 +2,4 @@ bower.json
component.json
.npmignore
.travis.yml
benchmark/
5 changes: 5 additions & 0 deletions benchmark/.eslintrc
@@ -0,0 +1,5 @@
{
"rules": {
"no-console": 0,
},
}
142 changes: 142 additions & 0 deletions benchmark/arraying.js
@@ -0,0 +1,142 @@
'use strict';

require('console.table');

var comma = require('comma-number');
var Benchmark = require('benchmark');

Benchmark.options.initCount = 100;
Benchmark.options.minSamples = 100;

// useful when altering tests, causes thing to run quickly
// Benchmark.options.initCount = 1
// Benchmark.options.minSamples = 1
// Benchmark.options.minTime = -1
// Benchmark.options.maxTime = -1

var singles = function singles(fn) {
return function () {
return fn('1', '2');
};
};

var arrayThenSingle = function arrayThenSingle(fn) {
return function () {
return fn([
'1', '11', '111'
], '2');
};
};

var singleThenArray = function singleThenArray(fn) {
return function () {
return fn('1', [
'2', '22', '222'
]);
};
};

var arrays = function arrays(fn) {
return function () {
return fn([
'1', '11', '111'
], [
'2', '22', '222'
]);
};
};

var methods = [
function original(a, b) {
return [].concat(a, b);
},

function consolidated(a, b) {
return [].concat(a, b);
},

function awkward(a, b) {
return Array.prototype.concat(a, b);
},

// this isn't able to handle the second arg being an array.
function halfway(a, b) {
if (Array.isArray(a)) {
a.push(b);
return a;
} else {
return [a, b];
}
},

function mutating(a, b) {
// we always use both of these, so, let's calculate them now
var firstIsArray = Array.isArray(a);
var secondIsArray = Array.isArray(b);

// mutate `a` to append `b` and then return it
if (firstIsArray) {
if (secondIsArray) {
a.push.apply(a, b);
} else {
a.push(b);
}
return a;
}

// mutate `b` to prepend `a` and then return it
if (secondIsArray) {
b.unshift(a);
return b;
}

// neither are arrays, so, create a new array with both
return [a, b];
}
];

methods[0].style = '[].concat(a).concat(b)';
methods[1].style = '[].concat(a, b)';
methods[2].style = 'Array.prototype.concat(a, b)';
methods[3].style = 'Array.isArray halfway';
methods[4].style = 'Array.isArray both';

var suite = new Benchmark.Suite();
var results = [];

methods.forEach(function (method, index) {
results.push([method.style]);

suite.add(method.style + ' with 2 non-arrays', singles(method));
suite.add(method.style + ' with an array then a non-array', arrayThenSingle(method));

if (index !== 3) {
suite.add(method.style + ' with a non-array then an array', singleThenArray(method));
suite.add(method.style + ' with 2 arrays', arrays(method));
}
});

var row = 0;

suite.on('cycle', function (event) {
var its = event.target;

console.log('completed', its.name);

results[row].push(comma(its.hz.toFixed(0)) + ' (+-' + its.stats.rme.toFixed(2) + '%)');

if (results[row].length === 5 || (row === 3 && results[row].length === 3)) {
row += 1;
}
});

suite.on('complete', function () {
console.log();
console.table([
'Name', '" / "', '[] / "', '" / []', '[] / []'
], results);
});

suite.run({
async: false
});
23 changes: 22 additions & 1 deletion lib/utils.js
Expand Up @@ -212,7 +212,28 @@ var isBuffer = function isBuffer(obj) {
};

var combine = function combine(a, b) {
return [].concat(a, b);
// we always use both of these, so, let's calculate them now
var firstIsArray = Array.isArray(a);
var secondIsArray = Array.isArray(b);

// mutate `a` to append `b` and then return it
if (firstIsArray) {
if (secondIsArray) {
a.push.apply(a, b);
} else {
a.push(b);
}
return a;
}

// mutate `b` to prepend `a` and then return it
if (secondIsArray) {
b.unshift(a);
return b;
}

// neither are arrays, so, create a new array with both
return [a, b];
};

module.exports = {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -25,7 +25,10 @@
"dependencies": {},
"devDependencies": {
"@ljharb/eslint-config": "^13.0.0",
"benchmark": "^2.1.4",
"browserify": "^16.2.2",
"comma-number": "^2.0.0",
"console.table": "^0.10.0",
"covert": "^1.1.0",
"editorconfig-tools": "^0.1.1",
"eslint": "^5.6.0",
Expand Down