访问对象中数据的复杂性 [英] Complexity of accessing data in an object

查看:85
本文介绍了访问对象中数据的复杂性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我作为日常工作的一部分工作的一些项目中,我需要访问非常大的JS对象中的数据(大约数千个键值对)。我正在努力提高代码的效率,所以我提出了几个问题:

In some of the projects I'm working on as part of my day job, I need to access data in very large JS objects (on the order of thousands of key-value pairs). I'm trying to improve the efficiency of my code, so I came up with a few questions:


  1. JS的运行时复杂性是什么访问这样的对象中的字段时? 我最初的预感是它是O(n)

  2. 通过点表示法或括号表示法访问时有区别吗? (例如 obj.field vs obj [field]

  3. 我'猜测不同的运行时引擎有不同的答案 - 我可以看到它们之间的区别吗?

  1. What is the runtime complexity of JS when accessing a field in such an object? My initial hunch is that it's O(n)
  2. Is there a difference when accessing through dot notation or bracket notation? (e.g. obj.field vs obj[field])
  3. I'm guessing there is a different answer for different runtime engines - is there a place where I can see the difference between them?


推荐答案

Javascript对象实际上是Hashes,所以所有引擎的复杂度都是 O(1)

Javascript objects are actually Hashes, so the complexity is O(1) for all engines.

obj.field obj ['field'] 的别名,所以他们有相同的表现。

obj.field is an alias for obj['field'], so they have the same performances.

您可以在此处找到一些JS哈希性能测试,很遗憾,仅适用于您的浏览器引擎。

You can find some JS hashes performance tests here, unfortunately only for your browser engine.

这篇关于访问对象中数据的复杂性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆