Add _.isEqual benchmarks.

Former-commit-id: 5b3ee06ea3f31f10e8fac96da8766cc1c4c2d3bb
This commit is contained in:
John-David Dalton
2012-07-13 04:17:38 -04:00
parent 6c4b4b392b
commit 4f78a06993
2 changed files with 118 additions and 40 deletions

View File

@@ -76,6 +76,8 @@
lodash.extend(Benchmark.options, { lodash.extend(Benchmark.options, {
'async': true, 'async': true,
'maxTime': 1,
'minSamples': 30,
'setup': function() { 'setup': function() {
var window = Function('return this || global')(), var window = Function('return this || global')(),
_ = window._, _ = window._,
@@ -83,19 +85,29 @@
var index, var index,
length, length,
belt = /Lo-Dash/.test(this.name) ? lodash : _, belt = this.name == 'Lo-Dash' ? lodash : _,
limit = 20, limit = 20,
numbers = Array(limit),
object = {}, object = {},
objects = Array(limit), objects = Array(limit),
numbers = Array(limit),
fourNumbers = [5, 25, 10, 30], fourNumbers = [5, 25, 10, 30],
nestedNumbers = [1, [2], [3, [[4]]]], nestedNumbers = [1, [2], [3, [[4]]]],
twoNumbers = [12, 21]; twoNumbers = [12, 21];
var object2 = {},
objects2 = Array(limit),
numbers2 = Array(limit),
nestedNumbers2 = [1, [2], [3, [[4]]]];
for (index = 0, length = limit; index < length; index++) { for (index = 0, length = limit; index < length; index++) {
numbers[index] = index; numbers[index] = index;
numbers2[index] = index;
object['key' + index] = index; object['key' + index] = index;
object2['key' + index] = index;
objects[index] = { 'num': index }; objects[index] = { 'num': index };
objects2[index] = { 'num': index };
} }
if (typeof isBindAll != 'undefined') { if (typeof isBindAll != 'undefined') {
@@ -108,19 +120,33 @@
} }
} }
var ctor = function() { }; var ctor = function() {};
var func = function(greeting, punctuation) { var func = function(greeting, punctuation) {
return greeting + ', ' + this.name + (punctuation || '.'); return greeting + ', ' + this.name + (punctuation || '.');
}; };
var lodashBoundNormal = lodash.bind(func, { 'name': 'moe' }), var objectOfPrimitives = {
lodashBoundCtor = lodash.bind(ctor, { 'name': 'moe' }), 'boolean': true,
lodashBoundPartial = lodash.bind(func, { 'name': 'moe' }, 'hi'); 'number': 1,
'string': 'a'
};
var _boundNormal = _.bind(func, { 'name': 'moe' }), var objectOfObjects = {
_boundCtor = _.bind(ctor, { 'name': 'moe' }), 'boolean': new Boolean(true),
_boundPartial = _.bind(func, { 'name': 'moe' }, 'hi'); 'number': new Number(1),
'string': new String('a')
};
var contextObject = { 'name': 'moe' };
var lodashBoundNormal = lodash.bind(func, contextObject),
lodashBoundCtor = lodash.bind(ctor, contextObject),
lodashBoundPartial = lodash.bind(func, contextObject, 'hi');
var _boundNormal = _.bind(func, contextObject),
_boundCtor = _.bind(ctor, contextObject),
_boundPartial = _.bind(func, contextObject, 'hi');
var tplData = { var tplData = {
'header1': 'Header1', 'header1': 'Header1',
@@ -200,15 +226,17 @@
'</ul>' + '</ul>' +
'</div>'; '</div>';
var settingsObject = { 'variable': 'data' };
var lodashTpl = lodash.template(tpl), var lodashTpl = lodash.template(tpl),
lodashTplWithEvaluate = lodash.template(tplWithEvaluate), lodashTplWithEvaluate = lodash.template(tplWithEvaluate),
lodashTplVerbose = lodash.template(tplVerbose, null, { 'variable': 'data' }), lodashTplVerbose = lodash.template(tplVerbose, null, settingsObject),
lodashTplVerboseWithEvaluate = lodash.template(tplVerboseWithEvaluate, null, { 'variable': 'data' }); lodashTplVerboseWithEvaluate = lodash.template(tplVerboseWithEvaluate, null, settingsObject);
var _tpl = _.template(tpl), var _tpl = _.template(tpl),
_tplWithEvaluate = _.template(tplWithEvaluate), _tplWithEvaluate = _.template(tplWithEvaluate),
_tplVerbose = _.template(tplVerbose, null, { 'variable': 'data' }), _tplVerbose = _.template(tplVerbose, null, settingsObject),
_tplVerboseWithEvaluate = _.template(tplVerboseWithEvaluate, null, { 'variable': 'data' }); _tplVerboseWithEvaluate = _.template(tplVerboseWithEvaluate, null, settingsObject);
var wordToNumber = { var wordToNumber = {
'one': 1, 'one': 1,
@@ -397,6 +425,18 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
suites.push(
Benchmark.Suite('`_.difference`')
.add('Lo-Dash', function() {
lodash.difference(numbers, fourNumbers, twoNumbers);
})
.add('Underscore', function() {
_.difference(numbers, fourNumbers, twoNumbers);
})
);
/*--------------------------------------------------------------------------*/
suites.push( suites.push(
Benchmark.Suite('`_.each` iterating an array') Benchmark.Suite('`_.each` iterating an array')
.add('Lo-Dash', function() { .add('Lo-Dash', function() {
@@ -555,18 +595,6 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
suites.push(
Benchmark.Suite('`_.difference`')
.add('Lo-Dash', function() {
lodash.difference(numbers, fourNumbers, twoNumbers);
})
.add('Underscore', function() {
_.difference(numbers, fourNumbers, twoNumbers);
})
);
/*--------------------------------------------------------------------------*/
suites.push( suites.push(
Benchmark.Suite('`_.groupBy` with `callback` iterating an array') Benchmark.Suite('`_.groupBy` with `callback` iterating an array')
.add('Lo-Dash', function() { .add('Lo-Dash', function() {
@@ -633,6 +661,58 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
suites.push(
Benchmark.Suite('`_.isEqual` comparing primitives and objects (edge case)')
.add('Lo-Dash', function() {
lodash.isEqual(objectOfPrimitives, objectOfObjects);
})
.add('Underscore', function() {
_.isEqual(objectOfPrimitives, objectOfObjects);
})
);
suites.push(
Benchmark.Suite('`_.isEqual` comparing arrays')
.add('Lo-Dash', function() {
lodash.isEqual(numbers, numbers2);
})
.add('Underscore', function() {
_.isEqual(numbers, numbers2);
})
);
suites.push(
Benchmark.Suite('`_.isEqual` comparing nested arrays')
.add('Lo-Dash', function() {
lodash.isEqual(nestedNumbers, nestedNumbers2);
})
.add('Underscore', function() {
_.isEqual(nestedNumbers, nestedNumbers2);
})
);
suites.push(
Benchmark.Suite('`_.isEqual` comparing arrays of objects')
.add('Lo-Dash', function() {
lodash.isEqual(objects, objects2);
})
.add('Underscore', function() {
_.isEqual(objects, objects2);
})
);
suites.push(
Benchmark.Suite('`_.isEqual` comparing objects')
.add('Lo-Dash', function() {
lodash.isEqual(object, object2);
})
.add('Underscore', function() {
_.isEqual(object, object2);
})
);
/*--------------------------------------------------------------------------*/
suites.push( suites.push(
Benchmark.Suite('`_.keys` (uses native `Object.keys` if available)') Benchmark.Suite('`_.keys` (uses native `Object.keys` if available)')
.add('Lo-Dash', function() { .add('Lo-Dash', function() {

View File

@@ -2698,20 +2698,18 @@
sample = bench.stats.sample; sample = bench.stats.sample;
/** /**
* Adds a number of clones to the queue. * Adds a clone to the queue.
*/ */
function enqueue(count) { function enqueue() {
while (count--) { queue.push(bench.clone({
queue.push(bench.clone({ '_original': bench,
'_original': bench, 'events': {
'events': { 'abort': [update],
'abort': [update], 'cycle': [update],
'cycle': [update], 'error': [update],
'error': [update], 'start': [update]
'start': [update] }
} }));
}));
}
} }
/** /**
@@ -2817,14 +2815,14 @@
} }
// if time permits, increase sample size to reduce the margin of error // if time permits, increase sample size to reduce the margin of error
if (queue.length < 2 && !maxedOut) { if (queue.length < 2 && !maxedOut) {
enqueue(1); enqueue();
} }
// abort the invoke cycle when done // abort the invoke cycle when done
event.aborted = done; event.aborted = done;
} }
// init queue and begin // init queue and begin
enqueue(minSamples); enqueue();
invoke(queue, { invoke(queue, {
'name': 'run', 'name': 'run',
'args': { 'async': async }, 'args': { 'async': async },