I know that Python, Perl, Java, Lua and obviously C (as it's the only array that's in ANSI standard afaik) support faster looking of numerically indexed arrays than doing a hash lookup or anything like that. Does Javascript also?
As an example in code of what I mean, in the case of Perl:
for(i=0;i<10;i++)
{
# Do something
}
is slower than:
for (1..9)
{
# do something else
}
or:
@var = (1,2,3)
foreach(@var)
{
print $_; # look I'm fancy
}
and faster than:
foreach my $key (keys %hash)
{
print $_; # Look I'm fancy
}
and in python, given the following:
class thisClass:
def methodOne(i):
return i+1
thisDict = {
'number': 1
}
These two operations are similar in speed because both involve a hash lookup into a same-widthed hash:
thisObject = new thisClass
i = thisObject.methodOne(1)
and:
i = 1+thisDict['1'];
Both of these kinds of lookups are slower than this:
thisTuple = (1,)
i = thisTuple[0]+1
While this:
thisArray = [1,2]
for i in thisArray:
print i
is iterated faster than:
thisTuple = (1,2)
for i in thisTuple:
print i
by which I mean lists iterate faster than tuples.
Does Javascript support numerically indexed arrays differently than associatinve arrays? I don't believe Javascript supports more than arrays and associative arrays, but if it does how do they compare to the two speed wise?
(1)is not a tuple (1,or(1,)is). There is no difference in iteration speed between tuples and lists.thisObjectis a class, so (in Python 2)thisObject.methodOne(1)will raise aTypeError. The key inthisDictis 'number', not '1'. Tuple indices start at 0, so the first element ofthisTupleisthisTuple[0], notthisTuple[1].