From c910a95ee452dec3d8e2145685f547cb62e5e921 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 15 May 2012 21:43:48 +0200 Subject: [PATCH] Add Node.js benchmark. Fixes #8 Former-commit-id: b60a09b6f0ed1d0880fb83f519c3755fa14cd625 --- benchmark/test.js | 150 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 + 2 files changed, 153 insertions(+) create mode 100644 benchmark/test.js diff --git a/benchmark/test.js b/benchmark/test.js new file mode 100644 index 000000000..99dc2eeed --- /dev/null +++ b/benchmark/test.js @@ -0,0 +1,150 @@ +/*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 ); \ No newline at end of file diff --git a/package.json b/package.json index 42768ad24..2cf201271 100644 --- a/package.json +++ b/package.json @@ -38,5 +38,8 @@ "directories": { "doc": "./doc", "test": "./test" + }, + "devDependencies": { + "colors": "~0.6.0" } } \ No newline at end of file