LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-16-2012, 10:09 PM   #1
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Non-associative arrays in JavaScript ?


Hello,

reading about JavaScript I found that arrays in it are associative.

Conceptually it's sufficient, i.e. arrays with numeric index can be mapped onto associative arrays using the numeric index (stringified ?) as hash key.

However, there are performance and memory footprint considerations.

With associative arrays hash function should be called per access, and from my experience in Perl storing data in hashes takes more memory - which is not surprising - buckets and stuff ( https://en.wikipedia.org/wiki/Hash_table ).

I also know that typed arrays are being introduced into JavaScript ( https://developer.mozilla.org/en-US/...t_typed_arrays ). But, IIUC, not all JavaScript engines support typed arrays - the latter do use numeric index like in "C".

So, are there other than typed non-associative arrays (the ones using numeric index directly) ? Perhaps as an add-on module and/or bindings to some code written in "C" ?

Thanks in advance.

Last edited by Sergei Steshenko; 11-16-2012 at 10:10 PM. Reason: numeric index, hash key
 
Old 11-17-2012, 04:41 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,119

Rep: Reputation: 218Reputation: 218Reputation: 218
Hi

The typed arrays are supported in all new browsers supporting HTML5. The only problem would be old Internet Explorer versions. Only IE 10 supports it.

I think it wasn't really necessary before WebGL. Traditionally, we would use Ajax calling a database on a server to deal with lots of data.

If all you need is a lookup in a big data set, you could write the data as a long string and use charCodeAt, but changing the data would be a problem.

The only thing I can think of for old browsers is using ActionScript (flash) or something. Arrays in ActionScript are always indexed by integer, but they're sparse arrays, so I'm really not sure about how it does the lookups.

Here's a good guide by the way:

http://www.html5rocks.com/en/tutoria.../typed_arrays/
 
1 members found this post helpful.
Old 11-17-2012, 11:14 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Original Poster
Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by Guttorm View Post
Hi

The typed arrays are supported in all new browsers supporting HTML5. The only problem would be old Internet Explorer versions. Only IE 10 supports it.

I think it wasn't really necessary before WebGL. Traditionally, we would use Ajax calling a database on a server to deal with lots of data.

If all you need is a lookup in a big data set, you could write the data as a long string and use charCodeAt, but changing the data would be a problem.

The only thing I can think of for old browsers is using ActionScript (flash) or something. Arrays in ActionScript are always indexed by integer, but they're sparse arrays, so I'm really not sure about how it does the lookups.

Here's a good guide by the way:

http://www.html5rocks.com/en/tutoria.../typed_arrays/
I meant Qt QML JavaScript which, AFAIK, does not yet support typed arrays. It looks like Qt developers are considering moving to V8 JavaScript Engine, Node.js, etc.

Anyway, the question is more general than just typed arrays. I am looking for elastic non-associative arrays which can store elements of "any" type - like Perl arrays do.
 
Old 11-17-2012, 11:17 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Original Poster
Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by Guttorm View Post
...
Here's a good guide by the way:

http://www.html5rocks.com/en/tutoria.../typed_arrays/
Thanks, a nice guide.
 
Old 11-17-2012, 12:48 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,396

Rep: Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814
Quote:
Originally Posted by Sergei Steshenko View Post
reading about JavaScript I found that arrays in it are associative.
They are not (MDN - Array):
Quote:
JavaScript arrays are zero-indexed; the first element of an array is actually at index 0...
Also linked from that page: JavaScript “Associative Arrays” Considered Harmful.
 
1 members found this post helpful.
Old 11-17-2012, 01:47 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Original Poster
Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by ntubski View Post
They are not (MDN - Array):


Also linked from that page: JavaScript “Associative Arrays” Considered Harmful.
Thanks for clarification - JavaScript is not as bad as I thought it was reading unreliable sources. And even is consistent with Perl regarding [...] vs {...}:

Quote:
So I will say it again: Array is not meant to be used for key/value pairs. Luckily, there is a dead-simple way to fix this. In the above example, you need only change Array to Object. (Or, if you’re using literal syntax, change [] to {}.) There. Your wrong code is no longer wrong, and it took only a little more work than a simple find-and-replace.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Could someone please explain the concept of associative arrays in AWK programming? AJAY E Linux - Newbie 6 05-27-2012 07:01 PM
LXer: Bash Associative Arrays LXer Syndicated Linux News 0 04-28-2010 10:20 PM
LXer: How To Fake Associative Arrays In Bash LXer Syndicated Linux News 1 06-05-2008 11:01 AM
Javascript External Arrays swatward Programming 1 04-21-2005 04:47 AM
using associative arrays corbis_demon Programming 0 01-25-2005 12:47 AM


All times are GMT -5. The time now is 12:38 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration