LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Non-associative arrays in JavaScript ? (https://www.linuxquestions.org/questions/programming-9/non-associative-arrays-in-javascript-4175437514/)

Sergei Steshenko 11-16-2012 10:09 PM

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.

Guttorm 11-17-2012 04:41 AM

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/

Sergei Steshenko 11-17-2012 11:14 AM

Quote:

Originally Posted by Guttorm (Post 4831282)
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.

Sergei Steshenko 11-17-2012 11:17 AM

Quote:

Originally Posted by Guttorm (Post 4831282)
...
Here's a good guide by the way:

http://www.html5rocks.com/en/tutoria.../typed_arrays/

Thanks, a nice guide.

ntubski 11-17-2012 12:48 PM

Quote:

Originally Posted by Sergei Steshenko (Post 4831149)
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.

Sergei Steshenko 11-17-2012 01:47 PM

Quote:

Originally Posted by ntubski (Post 4831477)
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.


All times are GMT -5. The time now is 07:08 AM.