I'm getting more and more into programming js in a functional manner which makes heavy use of multiple returns via tuples (especially useful with recursion). Also, a pattern that I use often is creating simple temporary values for projections
_.chain(arr) .filter(...) .map(function(x){ return [x.foo, getSomething(x.bar)] }) .filter(...) .reduce(...)
Usually I see tuples in js implemented with arrays [a, b]
though of course an object - while annoying to write would be just as valid: {a: a, b: b}
.
So for this sort of use-case (creation and light usage of a bunch of temporary objects) I'm curious if there's a significant performance difference so I set up this jsperf.
Object tuple test:
var all = [] for(var x = 0; x < 100; x+=1) for(var y = 0; y < 100; y+=1) all.push({x: x, y: y}) var mapped = [] for(var i=0; i < all.length; i+=1) mapped.push({x: all[i].x, y: all[i].y})
versus array tuple test
var all = [] for(var x = 0; x < 100; x+=1) for(var y = 0; y < 100; y+=1) all.push([x, y]) var mapped = [] for(var i=0; i < all.length; i+=1) mapped.push([all[i][0], all[i][1]])
What do people think? Is this a sufficient test for the concept?