2
  1. var key = 'a'; map[key] = 'value'; 
  2. map['a'] = 'value'; 

In Java this is optimized automatically during compilation. I want to know if any JS compiler does such optimization on its own.

2
  • 3
    That's not a hashmap, that's an object. Commented Oct 3, 2018 at 11:27
  • 3
    "I want to know if JS does such optimization on its own." — JS is a programming language, not a compiler. If any optimization is done, it is done in the compiler you use. Commented Oct 3, 2018 at 11:29

2 Answers 2

0

This answer is relevant here; there's no difference in performance as one is an alias of the other. You can see that these have the same performance by testing it:

var objectTest = { a: 1, } console.time('dot'); for (var i = 0; i < 100000000; i++) { objectTest.a = objectTest.a + 1; } console.timeEnd('dot'); objectTest = { a: 1, } console.time('bracket'); for (var i = 0; i < 100000000; i++) { objectTest['a'] = objectTest['a'] + 1; } console.timeEnd('bracket');

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

Comments

-2

Hence, there's no compiler, the chance of any optimization is very low.

var key = 'a'; map['a'] = 'value'; 

I think interpreter sees [a] as a normal variable thus, it is kept in the heap. So, both of this access types are equal (costly speaking). On the other hand, using static statements are always should be avoided.

1 Comment

There are compilers. Most people running JS (including people visiting webpages that include JS) use one. softwareengineering.stackexchange.com/questions/138521/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.