8

The question title almost says it all: do longer keys make for slower lookup? Is:

someObj["abcdefghijklmnopqrstuv"]

Slower than:

someObj["a"]

Another sub-question is whether the type of the characters in the string used as key matters. Are alphanumeric key-strings faster?

I tried to do some research; there doesn't seem to be much info online about this. Any help/insight would be extremely appreciated.

3
  • 2
    Why not run some tests on jsPerf? Commented Dec 18, 2012 at 19:40
  • 2
    jsperf.com/array-key-length-performance-impact Commented Dec 18, 2012 at 19:41
  • 1
    @j08691 Well that's a good idea, but I generically am not a fan of taking jsPerf-tests as conclusive evidence. There are many specifics that tend to affect the results. I just want to know how common implementations (V8, etc.) are theoretically expected to perform. Commented Dec 18, 2012 at 19:41

2 Answers 2

6

In general no. In the majority of languages, string literals are 'interned', which hashes them and makes their lookup much faster. In general, there may be some discrepancies between different javascript engines, but overall if they're implemented well (cough IE cough), it should be fairly equal. Especially since javascript engines are constantly being developed, this is (probably) an easy thing to optimize, and the situation will improve over time.

However, some engines also have limits on the length of strings that are interned. YMMV on different browsers. We can also see some insight from the jsperf test (linked in comments for the question). Firefox obviously does much more aggressive interning.

As for the types of characters, the string is treated as just a bunch bytes no matter the charset, so that probably won't matter either. Engines might optimize keys that can be used in dot notation but I don't have any evidence for that.

Sign up to request clarification or add additional context in comments.

8 Comments

I see, interesting. Well what about the second part of my question? Does the type of characters in the key matter?
Thanks! I was actually hoping for some insight by someone who had taken a look on the source code of some JavaScript engine such as V8 -- but your explanation makes it pretty clear. Thanks again.
The only thing that more complex characters will do is take up more space (guessing that engines use utf-8 as their internal representation, which takes up 1-6 bytes per character).
Yeah, I haven't actually looked at the code of a javascript engine (I might someday); this is just stuff you would/should learn from getting a CS degree.
@foivall actually chrome was really slow with using objects in dict mode before but has gotten much better. And as i read the original question his keys are not indicative of key ref of an object but i see your point. I will change it into 2 objects with optimizable object types and but different key lenghts.
|
5

The performance is the same if we are talking about Chrome which uses V8 javascript engine. Based on V8 design specifications you can see from "fast property access" and "Dynamic machine code generation" that in the end those keys end up being compiled as any other c++ class variables.

3 Comments

Well, what about keys that have special non-alphanumeric characters? Those aren't valid C++ class variable names, as far as I know.
It doesn't get compiled down to c++ per-se, it gets compiled down to machine code. JIT'ing just throws a whole bunch of other things you have to consider; I'm mainly considering the classical interpretation style that Firefox's engine is based on. V8 is just a beast, since it takes a very different approach.
I guess the engine knows to set other names for these kind of keys (internally). What should be understood is that the keys are compiled in the same way the vars are compiled in machine code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.