Running array and hash in parallel 
March 3rd, 2007, 12:45 AM
| | | |
I'm using an array to store map features (name, lat, lon, caption,
etc), from which the user can then select an individual feature. The
problem is that when thousands of features are stored in the array,
looping over the entire array looking for a match is SLOW.
So I'm running a hash in parallel, where every time a feature is
pushed onto the array it's name is also added to the hash as an
identical key value pair. I then check if the key is defined in the
hash, and if it is, I want to use that feature's values from the
array. Problem is, I don't know the index number in the array for this
feature. Is there a way to look this up without looping over it, by
matching values between the array and hash?
Eg:
featureArray = new Array();
featureHash = new Object();
//assume lots of features pushed
featureArray.push({name:name, lat:lat, lon:lon, caption:caption});
featureHash[name] = name;
//more features pushed
function useSelectedFeature (name) {
if (featureHash[name] != undefined) {
//match featureArray.name to featureHash[name]
//use matched featureArray values
}
}
Thanks. | 
March 3rd, 2007, 01:05 AM
| | | | re: Running array and hash in parallel
On 3 Mar, 00:38, "IamIan" <ian...@gmail.comwrote: Quote:
I'm using an array to store map features (name, lat, lon, caption,
etc), from which the user can then select an individual feature. The
problem is that when thousands of features are stored in the array,
looping over the entire array looking for a match is SLOW.
>
So I'm running a hash in parallel, where every time a feature is
pushed onto the array it's name is also added to the hash as an
identical key value pair. I then check if the key is defined in the
hash, and if it is, I want to use that feature's values from the
array. Problem is, I don't know the index number in the array for this
feature. Is there a way to look this up without looping over it, by
matching values between the array and hash?
>
Eg:
>
featureArray = new Array();
featureHash = new Object();
//assume lots of features pushed
featureArray.push({name:name, lat:lat, lon:lon, caption:caption});
featureHash[name] = name;
//more features pushed
>
function useSelectedFeature (name) {
if (featureHash[name] != undefined) {
//match featureArray.name to featureHash[name]
//use matched featureArray values
}
>
}
>
Thanks.
| You are implementing a basic database here, does your app require you
not to use one already pre-written, such as MySql, if you /can/ use
mysql do so... instead of pushing data to the js array, push it back
to the server, the performance hit of 100ms retrieving the row you
need when you query the database which has 100 million rows in it will
be negligible even coupled with a slow connection, this way you don't
reinvent the wheel learning about the best hash and how to make it all
speedy. Think google maps here. SOme parts are cached, others are
released depending on context and liklihood you will need that data. | 
March 3rd, 2007, 03:15 AM
| | | | re: Running array and hash in parallel
On Mar 3, 10:38 am, "IamIan" <ian...@gmail.comwrote: Quote:
I'm using an array to store map features (name, lat, lon, caption,
etc), from which the user can then select an individual feature. The
problem is that when thousands of features are stored in the array,
looping over the entire array looking for a match is SLOW.
>
So I'm running a hash in parallel
| You mean an object. Quote:
, where every time a feature is
pushed onto the array it's name is also added to the hash as an
identical key value pair. I then check if the key is defined in the
hash, and if it is, I want to use that feature's values from the
array. Problem is, I don't know the index number in the array for this
feature. Is there a way to look this up without looping over it, by
matching values between the array and hash?
>
Eg:
>
featureArray = new Array();
featureHash = new Object();
//assume lots of features pushed
featureArray.push({name:name, lat:lat, lon:lon, caption:caption});
| The new element's index will be featureArray.length - 1. Quote: |
featureHash[name] = name;
| Why not store the feature's data in the object:
featureObj = {
name: {lat: lat, long: long, caption: '...'},
...
}
So you get:
featureOb[name].lat = ...
featureOb[name].long = ...
...
Why do two look-ups when one will do? Quote:
//more features pushed
>
function useSelectedFeature (name) {
if (featureHash[name] != undefined) {
| var feature;
if (featureObj[name]){
feature = featureObj[name];
/* use feature's properties */
}
You can probably abbreviate that to:
if (feature = featureObj[name]) {
/* use feature's properties */
}
--
Rob | 
March 4th, 2007, 08:15 AM
| | | | re: Running array and hash in parallel
On Mar 3, 8:08 am, "RobG" <r...@iinet.net.auwrote: Quote:
On Mar 3, 10:38 am, "IamIan" <ian...@gmail.comwrote:
> Quote:
I'm using an array to store map features (name, lat, lon, caption,
etc), from which the user can then select an individual feature. The
problem is that when thousands of features are stored in the array,
looping over the entire array looking for a match is SLOW.
| > Quote: |
So I'm running a hash in parallel
| >
You mean an object.
> Quote:
, where every time a feature is
pushed onto the array it's name is also added to the hash as an
identical key value pair. I then check if the key is defined in the
hash, and if it is, I want to use that feature's values from the
array. Problem is, I don't know the index number in the array for this
feature. Is there a way to look this up without looping over it, by
matching values between the array and hash?
| >> Quote:
featureArray = new Array();
featureHash = new Object();
//assume lots of features pushed
featureArray.push({name:name, lat:lat, lon:lon, caption:caption});
| >
The new element's index will be featureArray.length - 1.
> Quote: |
featureHash[name] = name;
| >
Why not store the feature's data in the object:
>
featureObj = {
name: {lat: lat, long: long, caption: '...'},
...
}
>
So you get:
>
featureOb[name].lat = ...
featureOb[name].long = ...
...
>
Why do two look-ups when one will do?
>> Quote:
function useSelectedFeature (name) {
if (featureHash[name] != undefined) {
| >
var feature;
if (featureObj[name]){
feature = featureObj[name];
/* use feature's properties */
}
>
You can probably abbreviate that to:
>
if (feature = featureObj[name]) {
/* use feature's properties */
}
>
--
Rob
| Hey,
You can use the array of objects as a hash itself (assuming name is a
unique property). Something like that:
featureArray = new Array();
featureArray[_name_of_feature_as_string] = {lat:lat, lon:lon,...};
Then checking if a particular feature exists is as simple as this:
if ( featureArray[_name_of_feature_as_string] )
and getting to its properties:
featureArray[_name_of_feature_as_string].lat
featureArray[_name_of_feature_as_string].lon
Let me know if this helps. | 
March 4th, 2007, 11:05 AM
| | | | re: Running array and hash in parallel
On Mar 4, 6:02 pm, "varun" <varungupt...@gmail.comwrote: Quote:
On Mar 3, 8:08 am, "RobG" <r...@iinet.net.auwrote: Quote:
On Mar 3, 10:38 am, "IamIan" <ian...@gmail.comwrote: Quote:
I'm using an array to store map features (name, lat, lon, caption,
etc), from which the user can then select an individual feature. The
problem is that when thousands of features are stored in the array,
looping over the entire array looking for a match is SLOW.
| | > Quote: Quote: |
So I'm running a hash in parallel
| | | [...] Quote: Quote: |
Why not store the feature's data in the object:
| | [...] Quote:
Hey,
>
You can use the array of objects as a hash itself (assuming name is a
unique property). Something like that:
>
featureArray = new Array();
featureArray[_name_of_feature_as_string] = {lat:lat, lon:lon,...};
>
Then checking if a particular feature exists is as simple as this:
>
if ( featureArray[_name_of_feature_as_string] )
>
and getting to its properties:
>
featureArray[_name_of_feature_as_string].lat
featureArray[_name_of_feature_as_string].lon
>
Let me know if this helps.
| No, it doesn't. Arrays should be used as arrays, not plain objects -
that's what Objects are for.
Using an Array as a plain object is discourage because you run the
risk that one of the properties you add will conflict with an existing
Array property, particularly where you have no control over the names
of the properties being added.
--
Rob | 
March 5th, 2007, 10:25 AM
| | | | re: Running array and hash in parallel
On Mar 4, 3:54 pm, "RobG" <r...@iinet.net.auwrote: Quote:
On Mar 4, 6:02 pm, "varun" <varungupt...@gmail.comwrote:
>
>
> Quote:
On Mar 3, 8:08 am, "RobG" <r...@iinet.net.auwrote: Quote:
On Mar 3, 10:38 am, "IamIan" <ian...@gmail.comwrote:
I'm using anarrayto store map features (name, lat, lon, caption,
etc), from which the user can then select an individual feature. The
problem is that when thousands of features are stored in thearray,
looping over the entirearraylooking for a match is SLOW.
| | > Quote: Quote: |
So I'm running ahashinparallel
| | [...] Quote: Quote: |
Why not store the feature's data in the object:
| | [...]> Quote:
You can use thearrayof objects as ahashitself (assuming name is a
unique property). Something like that:
| > Quote:
featureArray = newArray();
featureArray[_name_of_feature_as_string] = {lat:lat, lon:lon,...};
| > Quote: |
Then checking if a particular feature exists is as simple as this:
| > Quote: |
if ( featureArray[_name_of_feature_as_string] )
| > Quote: |
and getting to its properties:
| > Quote:
featureArray[_name_of_feature_as_string].lat
featureArray[_name_of_feature_as_string].lon
| > Quote: |
Let me know if this helps.
| >
No, it doesn't. Arrays should be used as arrays, not plain objects -
that's what Objects are for.
>
Using anArrayas a plain object is discourage because you run the
risk that one of the properties you add will conflict with an existingArrayproperty, particularly where you have no control over the names
of the properties being added.
>
--
Rob
|
Rob,
You're spot on about distinguishing associative arrays from numbered
arrays. Objects should be used for the first and Arrays for the
second. But Arrays are essentially Objects with a thin layer of
functionality built over. In your solution don't you still run the
risk of new properties conflicting with the existing Object
properties?
Is it a good idea to prefix an underscore to the property names in
order to avoid any conflict?
Varun | 
March 5th, 2007, 12:25 PM
| | | | re: Running array and hash in parallel
On Mar 5, 8:14 pm, "varun" <varungupt...@gmail.comwrote: Quote: |
On Mar 4, 3:54 pm, "RobG" <r...@iinet.net.auwrote:
| [...] Quote: Quote:
Using anArrayas a plain object is discourage because you run the
risk that one of the properties you add will conflict with an existingArrayproperty, particularly where you have no control over the names
of the properties being added.
| | Quote:
Rob,
>
You're spot on about distinguishing associative arrays from numbered
arrays. Objects should be used for the first and Arrays for the
second. But Arrays are essentially Objects with a thin layer of
functionality built over.
| Yes. Quote:
In your solution don't you still run the
risk of new properties conflicting with the existing Object
properties?
| Yes, but the chances are lower. Object and its prototype have only 8
native properties: prototype, constructor, toString, toLocaleString,
valueOf, hasOwnProperty, hasOwnPropertyOf and propertyIsEnumerable.
Array has a whole bunch more. Quote:
Is it a good idea to prefix an underscore to the property names in
order to avoid any conflict?
| Sounds OK to me; you could chose any valid character that isn't the
first character of a standard property name, say "X" or "A" or
whatever.
--
Rob | 
March 5th, 2007, 12:45 PM
| | | | re: Running array and hash in parallel
On Mar 5, 12:14 pm, "RobG" <r...@iinet.net.auwrote: Quote: |
On Mar 5, 8:14 pm, "varun" <varungupt...@gmail.comwrote:
| <snip> Quote: Quote:
>Is it a good idea to prefix an underscore to the property
>names in order to avoid any conflict?
| >
Sounds OK to me; you could chose any valid character that isn't the
first character of a standard property name, say "X" or "A" or
whatever.
| Underscore is the first character in a set JavaScript(tm) extensions,
such as - __proto__ -, so may not be the best choice. An unusual
character sequence may make a better prefix, possibly including a
space character and something in the unicode range above 256.
Variable prefixes have also been suggested, such as the decimal
characters of a current (at the point of first recording it)
millisecond time. That prefix is extremely unlikely to coincide with
anything unexpected but still exposed in a javascript implementation,
and even if it does it will only do some once over a very long period.
Richard. | 
March 5th, 2007, 06:35 PM
| | | | re: Running array and hash in parallel
Thanks for the responses. I had thought of using the associative array
exclusively, but was curious if my original question was possible.
Sounds like it isn't. My last question concerns associative array use
with AJAX results.
If I try:
featureHash = new Object();
var name = AJAX result
featureHash[name] = name;
If fails, but if I modify the last line to be:
featureHash['"' + name + '"'] = '"' + name + '"';
It is successful. I confirmed my AJAX results are strings with typeof,
so I'm unclear on this one. Not a big deal but I'm interested in the
answer.
Thanks! | 
March 6th, 2007, 08:45 AM
| | | | re: Running array and hash in parallel
On Mar 5, 1:14 pm, "varun" <varungupt...@gmail.comwrote: Quote:
But Arrays are essentially Objects with a thin layer of
functionality built over.
| Yeah, right. And marijuana is really a tree, not a herb - they just
never let it grow big enough...
Object structure in javascript is expandable at run-time, so you can
use Object instance as dynamic storage of key/value elements (approx.
hash) . It is not an Object exclusive feature, with the same success
you can use any other instance in javascript, say
var hash = new Function;
hash['foo'] = bar;
The object is chosen because of the possibility of {"foo":"bar"}
constructs and because it has the minimum of native methods to worry
of being overriden.
It has nothing to do with the functional distinction of associative
array (map, hash, disctionary) and array. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,689 network members.
|