473,466 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Running array and hash in parallel

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.

Mar 3 '07 #1
9 1820
On 3 Mar, 00:38, "IamIan" <ian...@gmail.comwrote:
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.

Mar 3 '07 #2
On Mar 3, 10:38 am, "IamIan" <ian...@gmail.comwrote:
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 */
}
--
Rob

Mar 3 '07 #3
On Mar 3, 8:08 am, "RobG" <r...@iinet.net.auwrote:
On Mar 3, 10:38 am, "IamIan" <ian...@gmail.comwrote:
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 */
}

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

Mar 4 '07 #4
On Mar 4, 6:02 pm, "varun" <varungupt...@gmail.comwrote:
On Mar 3, 8:08 am, "RobG" <r...@iinet.net.auwrote:
On Mar 3, 10:38 am, "IamIan" <ian...@gmail.comwrote:
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.
--
Rob

Mar 4 '07 #5
On Mar 4, 3:54 pm, "RobG" <r...@iinet.net.auwrote:
On Mar 4, 6:02 pm, "varun" <varungupt...@gmail.comwrote:
On Mar 3, 8:08 am, "RobG" <r...@iinet.net.auwrote:
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.
So I'm running ahashinparallel
[...]
Why not store the feature's data in the object:
[...]
Hey,
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

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

Mar 5 '07 #6
On Mar 5, 8:14 pm, "varun" <varungupt...@gmail.comwrote:
On Mar 4, 3:54 pm, "RobG" <r...@iinet.net.auwrote:
[...]
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.
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.

--
Rob

Mar 5 '07 #7
On Mar 5, 12:14 pm, "RobG" <r...@iinet.net.auwrote:
On Mar 5, 8:14 pm, "varun" <varungupt...@gmail.comwrote:
<snip>
>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.

Mar 5 '07 #8
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!

Mar 5 '07 #9
VK
On Mar 5, 1:14 pm, "varun" <varungupt...@gmail.comwrote:
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.

Mar 6 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: R. Rajesh Jeba Anbiah | last post by:
I could see that it is possible to have hash array using objects like var hash = {"a" : "1", "b" : "2"}; Couldn't still findout how to declare hash array in Array. var arr = new Array("a" : "1",...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
5
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function...
4
by: David Bargna | last post by:
Hi I have a problem, I have a string which needs to be converted to a byte array, then have the string representation of this array stored in an AD attribute. This string attribute then has to...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
44
by: gokkog | last post by:
Hi there, There's a classic hash function to hash strings, where MULT is defined as "31": //from programming pearls unsigned int hash(char *ptr) { unsigned int h = 0; unsigned char *p =...
7
by: ianenis.tiryaki | last post by:
well i got this assignment which i dont even have a clue what i am supposed to do. it is about reading me data from the file and load them into a parallel array here is the question: Step (1) ...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.