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.

Ruturaj Patil
  • 608
  • 1
  • 10
  • 25
  • 3
    That's not a hashmap, that's an object. – melpomene Oct 03 '18 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. – Quentin Oct 03 '18 at 11:29

2 Answers2

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');
OliverRadini
  • 6,238
  • 1
  • 21
  • 46
-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.

Laws
  • 19
  • 6
  • There are compilers. Most people running JS (including people visiting webpages that include JS) use one. https://softwareengineering.stackexchange.com/questions/138521/is-javascript-interpreted-by-design – Quentin Oct 03 '18 at 12:05