What's the best way to store multiple values for jQuery's $.data()?

前端 未结 3 1144
南笙
南笙 2021-01-04 09:08

If storing multiple (10+) values on a large number of divs, is it more optimal to store them all in a single object, or as separate values?

Single Object:

3条回答
  •  情歌与酒
    2021-01-04 09:50

    If both approaches are as easy to use in your code, go for storing a single object. It avoids the extra overhead of calling the data() method every time you need a value. This way, you can fetch the mapping object with one call to data(), and then retrieve values from this object as many times as you want without having to call data() again.

    The data() method uses objects to map elements to keys/values under the hood, so it does not offer any performance improvements over managing a mapping object yourself.

提交回复
热议问题