It's very trivial (this is the bulk of it -- not showing everything to spare people from jumping over a huge message):
var f = function f(num) {
return num + 5;
}
var immutable = function immutable(arr) {
return arr.map(f);
}
var mutating = function mutating(arr) {
var i = 0;
var l = arr.length;
for(; i < l; i++) {
arr[i] = f(arr[i]);
}
return arr;
}
In my actual code, arr is a array of ascending numbers from 1 to 10,000 (different sized array each set of calls). I rebuild it before each call to immutable and mutating. I then run each of those functions, one after another 10,000 times. The intent is to make sure that there are no strange optimisations with a constant array and to even out the impact of the GC by interleaving the calls.
Unfortunately I tried to go to the link that you provided, but it just timed out :-(
Ah... it showed up. Hard to say what's going on there. I can't see the source code of how the benchmark works. But I do notice that you aren't returning the values. It is entirely possible that the code is being optimised out. I recommend returning the array. I also added up the elements of the array and checked that each implementation added up to the same number, just to make sure that the operation was actually performed.