Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
somewhat OT: creating a lookup hash in JS
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Felipe Gasper  
View profile  
 More options Oct 30 2012, 5:50 pm
From: Felipe Gasper <fel...@felipegasper.com>
Date: Tue, 30 Oct 2012 16:50:13 -0500
Local: Tues, Oct 30 2012 5:50 pm
Subject: somewhat OT: creating a lookup hash in JS
(Sorry, this is slightly OT.)

var the_array = ["foo", "bar", "baz", "qux"];

//Is there a simpler way to do the following than what s here?
var the_lookup = {};
the_array.forEach( function(i) { the_lookup.i = true } );

========

In Perl this is easy:
my @array = qw(foo bar baz qux);
my %lookup = map { $_ => 1 } @array;

Anything of the sort coming in JS, does anyone know? Maybe in some of
the newer ES5 goodies?

-FG


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mscdex  
View profile  
 More options Oct 30 2012, 6:26 pm
From: mscdex <msc...@gmail.com>
Date: Tue, 30 Oct 2012 15:25:48 -0700 (PDT)
Local: Tues, Oct 30 2012 6:25 pm
Subject: Re: somewhat OT: creating a lookup hash in JS
On Oct 30, 5:50 pm, Felipe Gasper <fel...@felipegasper.com> 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-Implement...

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Felipe Mobus  
View profile  
 More options Oct 30 2012, 6:59 pm
From: Felipe Mobus <fmo...@gmail.com>
Date: Tue, 30 Oct 2012 20:36:28 -0200
Local: Tues, Oct 30 2012 6:36 pm
Subject: Re: [nodejs] Re: somewhat OT: creating a lookup hash in JS
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;

--
Felipe Mobus
http://fmobus.wait4.org

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Felipe Mobus  
View profile  
 More options Oct 30 2012, 6:59 pm
From: Felipe Mobus <fmo...@gmail.com>
Date: Tue, 30 Oct 2012 20:42:22 -0200
Local: Tues, Oct 30 2012 6:42 pm
Subject: Re: [nodejs] Re: somewhat OT: creating a lookup hash in JS
pardon me for the (cur % 2) == 0 part, completely unnecessary to your case

--
Felipe Mobus
http://fmobus.wait4.org

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Trevor Norris  
View profile  
 More options Oct 30 2012, 7:02 pm
From: Trevor Norris <trev.nor...@gmail.com>
Date: Tue, 30 Oct 2012 16:02:37 -0700 (PDT)
Local: Tues, Oct 30 2012 7:02 pm
Subject: Re: somewhat OT: creating a lookup hash in JS

To supplement what mscdex has already posted, here are two more articles:

http://dailyjs.com/2012/10/15/preparing-for-esnext/
https://brendaneich.com/2012/10/harmony-of-dreams-come-true/

You'l want to check out Set's. Enable them with `node --harmony` at
runtime. Also, v8 doesn't support Set initialization with an Array. So
you'll have to use .add() for every element. But they are about 6 times
faster than using an Object lookup. Sort of like the following:

var the_set = new Set();
the_array.forEach(function(i) { the_set.add(i); });

As a side note, your syntax is incorrect. The forEach statement should look
like the following:

the_array.forEach(function(i) { the_lookup[i] = true; });


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ted Young  
View profile  
 More options Oct 30 2012, 7:35 pm
From: Ted Young <t...@radicaldesigns.org>
Date: Tue, 30 Oct 2012 16:35:43 -0700
Local: Tues, Oct 30 2012 7:35 pm
Subject: Re: [nodejs] Re: somewhat OT: creating a lookup hash in JS

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 <fmo...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rick Waldron  
View profile  
 More options Oct 31 2012, 11:34 am
From: Rick Waldron <waldron.r...@gmail.com>
Date: Wed, 31 Oct 2012 11:33:29 -0400
Local: Wed, Oct 31 2012 11:33 am
Subject: Re: [nodejs] Re: somewhat OT: creating a lookup hash in JS

On Tue, Oct 30, 2012 at 7:02 PM, Trevor Norris <trev.nor...@gmail.com>wrote:

> To supplement what mscdex has already posted, here are two more articles:

> http://dailyjs.com/2012/10/15/preparing-for-esnext/
> https://brendaneich.com/2012/10/harmony-of-dreams-come-true/

> You'l want to check out Set's. Enable them with `node --harmony` at
> runtime. Also, v8 doesn't support Set initialization with an Array. So
> you'll have to use .add() for every element. But they are about 6 times
> faster than using an Object lookup. Sort of like the following:

> var the_set = new Set();
> the_array.forEach(function(i) { the_set.add(i); });

Ugh, it's too bad v8's implementation is outdated, because the correct way
to do exactly this is:

var set = new Set(the_array); // creates a unique set with lookup API for
free! yay!

Rick


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »