0

Given the following object:

const myObject = {
  color0: '#000000',
  color1: '#FFFFFF',
  color2: '#AAAAAA',
  color3: '#000000'
}

Would myObject consume less memory to declare it like this?

const myObject = (() => {
  const color000000 = '#000000'

  return {
    color0: color000000,
    color1: '#FFFFFF',
    color2: '#AAAAAA',
    color3: color000000
  }
})()

The difference between the two is that since '#000000' is repeated multiple times, theoretically it would be more efficient to have them pointing to the same memory address.

VoidTwo
  • 569
  • 1
  • 7
  • 26
  • It's quite likely that the JS compiler automatically "interns" small string literals, so it's effectively doing what you want automatically. – Barmar Mar 29 '23 at 22:56

0 Answers0