var MAX = 1000000;
var arr = [], obj = {}, obj2 = {},foo, start;

for(i = 0; i < MAX; i++){
  arr.push(i.toString());
}

start = Date.now();
arr.reduce(function(obj2,i){
    obj2[i] = true;
    return obj2;
},obj2);
console.log('reduce',Date.now() - start);

start = Date.now();
arr.forEach(function(i){
    obj[i] = true;
});
console.log('forEach',Date.now() - start);
var MAX = 1000000;
var arr = [], obj = {}, obj2 = {},foo, start;

for(i = 0; i < MAX; i++){
  arr.push(i.toString());
}

start = Date.now();
arr.reduce(function(obj2,i){
    obj2[i] = true;
    return obj2;
},obj2);
console.log('reduce',Date.now() - start);

start = Date.now();
arr.forEach(function(i){
    obj[i] = true;
});
console.log('forEach',Date.now() - start);
var MAX = 1000000;
var arr = [], obj = {}, obj2 = {},foo, start;

for(i = 0; i < MAX; i++){
  arr.push(i.toString());
}

start = Date.now();
arr.reduce(function(obj2,i){
    obj2[i] = true;
    return obj2;
},obj2);
console.log('reduce',Date.now() - start);

start = Date.now();
arr.forEach(function(i){
    obj[i] = true;
});
console.log('forEach',Date.now() - start);
A quick speed test, comparing forEach and reduce:

var arr = [], obj = {}, obj2 = {}, start;
var MAX = 1000000;

for(i = 0; i < MAX; i++){
  arr.push(i.toString());
}

// test reduce
start = Date.now();
arr.reduce(function(obj,i){
    obj[i] = true;
    return obj;
},obj);
console.log('reduce',Date.now() - start);

// test forEach
start = Date.now();
arr.forEach(function(i){
    obj2[i] = true;
});
console.log('forEach',Date.now() - start);
var MAX = 1000000;
var arr = [], obj = {}, obj2 = {},foo, start;

for(i = 0; i < MAX; i++){
  arr.push(i.toString());
}

start = Date.now();
arr.reduce(function(obj2,i){
    obj2[i] = true;
    return obj2;
},obj2);
console.log('reduce',Date.now() - start);

start = Date.now();
arr.forEach(function(i){
    obj[i] = true;
});
console.log('forEach',Date.now() - start);
var MAX = 1000000;
var arr = [], obj = {}, obj2 = {},foo, start;

for(i = 0; i < MAX; i++){
  arr.push(i.toString());
}

start = Date.now();
arr.reduce(function(obj2,i){
    obj2[i] = true;
    return obj2;
},obj2);
console.log('reduce',Date.now() - start);

start = Date.now();
arr.forEach(function(i){
    obj[i] = true;
});
console.log('forEach',Date.now() - start);


results are:

in node:
reduce 169
forEach 147

in node if you reverse the order:
forEach 170
reduce 145

in firefox:
reduce 956
forEach 1906

reversed in firefox:
forEach 2057
reduce 944

If this is for the browser, reduce seems twice as fast in firefox.  In node 
they appear equivalent. 

forEach is about as concise as it gets, but hey, one line of code ain't bad. :)

Ted

On Oct 30, 2012, at 3:36 PM, Felipe Mobus <[email protected]> wrote:

> You could use ES5's array.reduce() method, but it's gonna be a bit ugly
> 
> [1,2,3,4,5,6,7,8].reduce(function(prev, cur) {
>  prev[cur] = (cur % 2) == 0;
>  return prev;
> }, {})
> 
> On Tue, Oct 30, 2012 at 8:25 PM, mscdex <[email protected]> wrote:
>> On Oct 30, 5:50 pm, Felipe Gasper <[email protected]> wrote:
>>> Anything of the sort coming in JS, does anyone know? Maybe in some of
>>> the newer ES5 goodies?
>> 
>> Does this help?: 
>> https://github.com/joyent/node/wiki/ECMA-5-Mozilla-Features-Implemented-in-V8
>> 
>> --
>> Job Board: http://jobs.nodejs.org/
>> Posting guidelines: 
>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> You received this message because you are subscribed to the Google
>> Groups "nodejs" group.
>> To post to this group, send email to [email protected]
>> To unsubscribe from this group, send email to
>> [email protected]
>> For more options, visit this group at
>> http://groups.google.com/group/nodejs?hl=en?hl=en
> 
> 
> 
> -- 
> Felipe Mobus
> http://fmobus.wait4.org
> 
> -- 
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: 
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to