Cleanup benchmark/benchmark.js, add firebug-lite, and add support for Narwhal, Rhino, and Ringo.

Former-commit-id: e08ede5bee3fdfbeb648b855dfb3082e66e68c0b
This commit is contained in:
John-David Dalton
2012-05-20 04:14:56 -04:00
parent c8f3b128cb
commit c2a1f50dc0
74 changed files with 37298 additions and 155 deletions

192
benchmark/benchmark.js Normal file
View File

@@ -0,0 +1,192 @@
(function(window) {
/** Use a single load function */
var load = typeof require == 'function' ? require : window.load;
/** Load Benchmark.js */
var Benchmark =
window.Benchmark || (
Benchmark = load('../vendor/benchmark.js/benchmark.js') || window.Benchmark,
Benchmark.Benchmark || Benchmark
);
/** Load Lo-Dash */
var lodash =
window.lodash || (
lodash = load('../lodash.js') || window._,
lodash = lodash._ || lodash,
lodash.noConflict()
);
/** Load Underscore */
var _ =
window._ || (
_ = load('../vendor/underscore/underscore.js') || window._,
_._ || _
);
/** Used to score Lo-Dash and Underscore performance */
var score = { 'lodash': 0, 'underscore': 0 };
/** Use a queue benchmark suites */
var suites = [];
/** Add `console.log()` support for Narwhal and RingoJS */
window.console || (window.console = { 'log': window.print });
/** Expose functions to the global object */
window._ = _;
window.Benchmark = Benchmark;
window.lodash = lodash;
/*--------------------------------------------------------------------------*/
lodash.extend(Benchmark.options, {
'async': true,
'setup': function() {
var window = Function('return this || global')(),
_ = window._,
lodash = window.lodash,
numbers = [],
object = {};
for (var index = 0; index < 20; index++) {
numbers[index] = index;
object['key' + index] = index;
}
var objects = lodash.map(numbers, function(n) {
return { 'num': n };
});
}
});
lodash.extend(Benchmark.Suite.options, {
'onStart': function() {
console.log('\n' + this.name + ':');
},
'onCycle': function(event) {
console.log(event.target.toString());
},
'onComplete': function() {
var fastest = this.filter('fastest').pluck('name');
if (fastest.length > 1) {
console.log('It\'s too close to call.');
} else {
console.log(fastest + ' is the fastest.');
}
score.lodash += Math.floor(1 / (this[0].stats.mean + this[0].stats.moe));
score.underscore += Math.floor(1 / (this[1].stats.mean + this[1].stats.moe));
// remove from current suite from queue
suites.shift();
if (suites.length) {
// run next suite
suites[0].run();
}
else {
// report results
if (score.lodash >= score.underscore) {
console.log('\nLo-Dash is ' + (score.lodash / score.underscore).toFixed(2) + 'x faster than Underscore.');
} else {
console.log('\nUnderscore is ' + (score.underscore / score.lodash).toFixed(2) + 'x faster than Lo-Dash.');
}
}
}
});
/*--------------------------------------------------------------------------*/
suites.push(
Benchmark.Suite('each array')
.add('Lo-Dash', function() {
var timesTwo = [];
lodash.each(numbers, function(num) {
timesTwo.push(num * 2);
});
})
.add('Underscore', function() {
var timesTwo = [];
_.each(numbers, function(num) {
timesTwo.push(num * 2);
});
})
);
/*--------------------------------------------------------------------------*/
suites.push(
Benchmark.Suite('each object')
.add('Lo-Dash', function() {
var timesTwo = [];
lodash.each(object, function(num) {
timesTwo.push(num * 2);
});
})
.add('Underscore', function() {
var timesTwo = [];
_.each(object, function(num) {
timesTwo.push(num * 2);
});
})
);
/*--------------------------------------------------------------------------*/
suites.push(
Benchmark.Suite('keys')
.add('Lo-Dash', function() {
lodash.keys( object );
})
.add('Underscore', function() {
_.keys( object );
})
);
/*--------------------------------------------------------------------------*/
suites.push(
Benchmark.Suite('map')
.add('Lo-Dash', function() {
lodash.map( objects, function( obj ) {
return obj.num;
});
})
.add('Underscore', function() {
_.map( objects, function( obj ) {
return obj.num;
});
})
);
/*--------------------------------------------------------------------------*/
suites.push(
Benchmark.Suite('pluck')
.add('Lo-Dash', function() {
lodash.pluck(objects, 'num');
})
.add('Underscore', function() {
_.pluck(objects, 'num');
})
);
/*--------------------------------------------------------------------------*/
suites.push(
Benchmark.Suite('values')
.add('Lo-Dash', function() {
lodash.values(objects);
})
.add('Underscore', function() {
_.values(objects);
})
);
/*--------------------------------------------------------------------------*/
// start suites
suites[0].run();
}(typeof global == 'object' && global || this));

34
benchmark/index.html Normal file
View File

@@ -0,0 +1,34 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Lo-Dash Benchmark Suite</title>
<style>
html, body {
margin: 0;
padding: 0;
height: 100%;
}
</style>
</head>
<body>
<script src="../lodash.js"></script>
<script>
var lodash = _.noConflict();
</script>
<script src="../vendor/underscore/underscore.js"></script>
<script src="../vendor/benchmark.js/benchmark.js"></script>
<script src="../vendor/firebug-lite/src/firebug-lite-debug.js"></script>
<script>
window.onload = function() {
var sibling = document.scripts[0],
script = document.createElement('script');
document.getElementById('FirebugUI').style.height = '100%';
script.src = 'benchmark.js';
sibling.parentNode.insertBefore(script, sibling);
};
</script>
</body>
</html>

7
benchmark/run-benchmark.sh Executable file
View File

@@ -0,0 +1,7 @@
cd "$(dirname "$0")"
for cmd in rhino ringo narwhal node; do
echo "Benchmarking in $cmd..."
$cmd benchmark.js
done
echo "Benchmarking in a browser..."
open index.html

View File

@@ -1,150 +0,0 @@
/*global _, lodash */
var Benchmark = require('../vendor/benchmark.js');
var colors = require('colors');
var count = {
underscore: 0,
lodash: 0
};
// Workaround node.js scoping issue
global.lodash = require('../');
global._ = require('../vendor/underscore');
Benchmark.options.setup = function() {
var objects, randomized;
var lodash = global.lodash;
var _ = global._;
var numbers = [];
var object = {};
var i = 0;
for ( ; i < 20; i++ ) {
numbers[ i ] = i;
object[ 'key' + i ] = i;
}
objects = _.map(numbers, function( n ) {
return { 'num': n };
});
randomized = _.sortBy(numbers, function() {
return Math.random();
});
};
function start() {
console.log( '\n' + this.name.bold );
}
function cycle( e ) {
console.log( e.target.toString().grey );
}
function complete() {
console.log( this.filter('fastest').pluck('name').toString().green + ' fastest' );
count.underscore += this[0].count;
count.lodash += this[1].count;
}
/* each */
Benchmark.Suite('each')
.add('Underscore', function() {
var timesTwo = [];
_.each( numbers, function( num ) {
timesTwo.push( num * 2 );
});
})
.add('Lodash', function() {
var timesTwo = [];
lodash.each( numbers, function( num ) {
timesTwo.push( num * 2 );
});
})
.on( 'start', start )
.on( 'cycle', cycle )
.on( 'complete', complete )
.run();
/* each object */
Benchmark.Suite('each object')
.add('Underscore', function() {
var timesTwo = [];
_.each( object, function( num ) {
timesTwo.push( num * 2 );
});
})
.add('Lodash', function() {
var timesTwo = [];
lodash.each( object, function( num ) {
timesTwo.push( num * 2 );
});
})
.on( 'start', start )
.on( 'cycle', cycle )
.on( 'complete', complete )
.run();
/* keys */
Benchmark.Suite('keys')
.add('Underscore', function() {
_.keys( object );
})
.add('Lodash', function() {
lodash.keys( object );
})
.on( 'start', start )
.on( 'cycle', cycle )
.on( 'complete', complete )
.run();
/* map */
Benchmark.Suite('map')
.add('Underscore', function() {
_.map( objects, function( obj ) {
return obj.num;
});
})
.add('Lodash', function() {
lodash.map( objects, function( obj ) {
return obj.num;
});
})
.on( 'start', start )
.on( 'cycle', cycle )
.on( 'complete', complete )
.run();
/* pluck */
Benchmark.Suite('pluck')
.add('Underscore', function() {
_.pluck( objects, 'num' );
})
.add('Lodash', function() {
lodash.pluck( objects, 'num' );
})
.on( 'start', start )
.on( 'cycle', cycle )
.on( 'complete', complete )
.run();
/* values */
Benchmark.Suite('values')
.add('Underscore', function() {
_.values( objects );
})
.add('Lodash', function() {
lodash.values( objects );
})
.on( 'start', start )
.on( 'cycle', cycle )
.on( 'complete', complete )
.run();
console.log( ('\nLodash is ' + ( count.lodash / count.underscore ).toFixed(2) + 'x faster than Underscore' ).green );