Running array and hash in parallel

I

IamIan

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.
 
S

shimmyshack

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.
 
R

RobG

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.
, 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.
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?
//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 */
}
 
V

varun

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.
, 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?

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.
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?
//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 */
}

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.
 
R

RobG

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
[...]
Why not store the feature's data in the object:
[...]
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.
 
V

varun

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.
So I'm running ahashinparallel [...]
Why not store the feature's data in the object:
[...]

You can use thearrayof objects as ahashitself (assuming name is a
unique property). Something like that:
featureArray = newArray();
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 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,

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
 
R

RobG

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.

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.

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.
 
R

Richard Cornford

On Mar 5, 8:14 pm, "varun" <[email protected]> wrote:

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.
 
I

IamIan

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!
 
V

VK

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top