3

What is difference between localStorage.getItem('item') vs localStorage.item is there any performance difference between those two method of accessing item in localStorage?

Is accessing item in localStorage like this: var i = localStorage.item faster than getItem function call?

namename
  • 83
  • 3
  • 1
    no difference, if you are not using keys like `getItem`, `setItem`, and other that exist on localStorage – Andrei Mar 25 '23 at 12:14
  • [Is localStorage.getItem('item') better than localStorage.item or localStorage\['item'\]?](https://stackoverflow.com/q/12632463) – Nick Parsons Mar 25 '23 at 12:31
  • @Andrei so if there is no difference then why i should use long getitem('i') version over .i? – Gurami Nikolaishvili Mar 25 '23 at 13:16
  • @GuramiNikolaishvili Who said you should? Read [my answer](http://stackoverflow.com/a/75841681/254343) substantiated _by the [HTML] standard_, don't go by opinions. "Programming by Stack Overflow" is part to blame for why so many Web pages, some of which once had changed hands for a lot of money, are so broken today. For a good reason we have _standard committees_ today, working out standards that _specify_ how the Web browser is to behave, so as to prevent people from getting their facts wrong, letting them instead get information from the proper source, and program accordingly. – Armen Michaeli Mar 25 '23 at 15:02

2 Answers2

0

there is nothing regarding with localStorage.item its always localStorage.getItem('itemname') & localStorage.setItem('itemname' , value) & localStorage.clear()

Lucifer
  • 81
  • 5
0

The HTML Standard specifies the getItem method as follows:

  1. If this's map[key] does not exist, then return null.
  2. Return this's map[key].

Further down from the Storage interface definition, there is the following that explains what this map is:

A Storage object has an associated:

map
A storage proxy map.

And then this:

The supported property names on a Storage object storage are the result of running get the keys on storage's map.

This at least confirms that using both getItem and property access boils down to accessing the map to obtain the value corresponding to the key -- meaning both getItem method and property access indeed use the same procedure so obtain the same value for a given key.

This is also alluded to from the [non-normative] note closer to the Storage interface specification, titled "For web developers":

value = storage.getItem (key)
value = storage[key]

Returns the current value associated with the given key, or null if the given key does not exist.

The "HTML Standard" I quoted from, is linked from the Mozilla Developer Network page on Storage class, among other places, which itself is discoverable through a Google search for localStorage. I recommend you bookmark the Mozilla Developer Network site and add it as a search engine so you can quickly discover relevant and important information on the Web APIs you use. Don't take MDN for granted though -- if in doubt or as habit, find the relevant standard they attempt to explain, and see if you can read the actual specification, to clear all doubt.

Armen Michaeli
  • 8,625
  • 8
  • 58
  • 95