Created
November 26, 2016 21:42
-
-
Save Tom910/41e6f150bc6d1325ebe75f6fe04245bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Benchmark = require('benchmark'); | |
var expect = require('expect'); | |
var _ = require('lodash'); | |
var R = require('ramda'); | |
var suite = new Benchmark.Suite; | |
var testArray = [1,2,3,4,5,6,7,8,9,10,11,12,13]; | |
var resultat = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26]; | |
suite | |
.add('For', () => forTest(testArray)) | |
.add('map native', () => mapNativeTest(testArray)) | |
.add('map lodash', () => mapLodashTest(testArray)) | |
.add('map ramda', () => mapRamdaTest(testArray)) | |
.on('cycle', event => console.log(String(event.target))) | |
.on('complete', function() {console.log('Fastest is ' + this.filter('fastest').map('name'))}) | |
.run({ 'async': true }); | |
function forTest(array) { | |
var length = array.length, | |
result = Array(length), | |
i = 0; | |
for(; i < length; i++) { | |
result[i] = array[i] * 2; | |
} | |
return result; | |
} | |
function mapNativeTest(arr) { | |
return arr.map(function(item) {return item * 2}) | |
} | |
function mapLodashTest(arr) { | |
return _.map(arr, function(item) {return item * 2}) | |
} | |
function mapRamdaTest(arr) { | |
return R.map(function(item) {return item * 2}, arr) | |
} | |
expect(forTest(testArray)).toEqual(resultat); | |
expect(mapNativeTest(testArray)).toEqual(resultat); | |
expect(mapLodashTest(testArray)).toEqual(resultat); | |
expect(mapRamdaTest(testArray)).toEqual(resultat); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment