-
-
Save joseluisq/f6047617323b941a49da1cee3f35dda0 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 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('forHoley', () => forHoley(testArray)) | |
.add('forPush', () => forPush(testArray)) | |
.add('map', () => mapTest(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 forHoley(array) { | |
var length = array.length, | |
result = Array(length); | |
for(var i = 0; i < length; i++) { | |
result[i] = array[i] * 2; | |
} | |
return result; | |
} | |
function forPush(array) { | |
var result = []; | |
for(var i = 0; i < array.length; i++) { | |
result[i] = array[i] * 2; | |
} | |
return result; | |
} | |
function mapTest(arr) { | |
return arr.map(function(item) {return item * 2}) | |
} | |
expect(forHoley(testArray)).toEqual(resultat); | |
expect(forPush(testArray)).toEqual(resultat); | |
expect(mapTest(testArray)).toEqual(resultat); | |
for(var i = 0; i < array.length; i++) { | |
result[i] = array[i] * 2; | |
} | |
return result; | |
} | |
function mapTest(arr) { | |
return arr.map(function(item) {return item * 2}) | |
} | |
expect(forTest(testArray)).toEqual(resultat); | |
expect(forSlowTest(testArray)).toEqual(resultat); | |
expect(mapTest(testArray)).toEqual(resultat); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment