Map vs Recursion

JavaScript performance comparison

Test case created by kylejeske

Preparation code

<script>
Benchmark.prototype.setup = function() {
// create an array of length 100 [1,2,3,4,5 ... 97,98,99,100]
const numberArray = Array.from({length: 2000}, (v,i) => i + 1);

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Double Numbers by Recursion
/**
* Translates an Array of Numbers to Double that using Recursion and some ES6 tricks.
* double-numbers.js
*/
((numbers=[]) => {

const doubleNumbers = ( collection = [], [ head, ...tail ] = collection ) =>
(collection.length === 0)
? []
: [].concat([head * 2], doubleNumbers(tail))

doubleNumbers(numbers);

})(numberArray);
pending…
Apples to Apples, while loop
(numbers => {
const multiplyNumbers = (numbers, n = 0) => {
while(n < numbers.length) numbers[n] = numbers[n] * 2, n++;
}
multiplyNumbers(numbers);
})(numberArray)
pending…
Double Numbers by Map
(numbers => {
const doubleNumbers = _ => _.map(n => n * 2);
doubleNumbers(numbers);
})(numberArray);
pending…
Faster Version Recursion
(numbers => {
const multiplyNumbers = (arr = [], p = (arr.length - 1)) => {
arr[p] = arr[p] * 2,
p--,
(p >= 0) ? multiplyNumbers(arr, p) : {}
};
multiplyNumbers(numbers);
})(numberArray);
pending…

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.