简体   繁体   English

JavaScript性能(typeof arr [i] ===“undefined”|| num <arr [i])

[英]JavaScript Performance (typeof arr[i] === “undefined” || num < arr[i])

I'm looking for a way to avoid the typeof check on every arr[i] 我正在寻找一种方式来避免typeof上的每个检查arr[i]

I need to accommodate the fact that arr[i] can be 0 我需要考虑到arr[i]可以为0的事实

I want to assign arr[i] = num if arr[i] is undefined or greater than num 如果arr[i] undefined或大于num我想分配arr[i] = num num

The way to answer performance questions is to run benchmarks. 回答性能问题的方法是运行基准测试。

There are four alternatives: 有四种选择:

  1. arr[i] === undefined || num < arr[i]

  2. typeof arr[i] === 'undefined' || num < arr[i]

  3. !(i in arr) || num < arr[i] !(i in arr) || num < arr[i] . !(i in arr) || num < arr[i] This assumes the undefined values are true "holes" (missing indexes), rather than indexes that are present but have a value of undefined . 这假定undefined值是真正的“漏洞”(缺少索引),而不是存在但具有undefined值的索引。

  4. !(num >= arr[i]) . !(num >= arr[i]) This takes advantage of the fact that comparisons with undefined return false . 这利用了与undefined比较返回false的事实。

1 and 2 are equivalent performance-wise. 1和2在性能方面是等效的。 3 is about 20 times slower. 3慢约20倍。 4 is about 20% slower. 4慢约20%。 Using isNaN as suggested in another answer is about 50% slower. 在另一个答案中建议使用isNaN大约慢50%。

Therefore, if you are interested purely in performance, use 1 or 2. If you want to save keystrokes, at a minor performance cost, use 4. However, 4 is also less readable and you probably will have to add a comment about how it handles undefined , which will negate any keystroke savings. 因此,如果您纯粹对性能感兴趣,请使用1或2.如果您想以较小的性能成本保存击键,请使用4.但是,4也不太可读,您可能需要添加关于它的注释处理undefined ,这将取消任何击键节省。

See http://jsperf.com/ways-to-check-for-undefined/4 . 请参见http://jsperf.com/ways-to-check-for-undefined/4

Try this: 试试这个:

arr[i] == undefined || num < arr[i]

or 要么

arr[i]=(arr[i] >= 0 && arr [i] < num) ? arr[i] : num;

Or 要么

arr[i]=(!isNaN(arr[i]) && arr [i] < num) ? arr[i] : num;

Checking something === something_else is as fast as it gets, but what does make your code slower is the fact that you are doing two things instead of one: 检查something === something_else的速度和它一样快,但是让你的代码变慢的原因是你做了两件事而不是一件事:

You are checking both typeof arr[i] === “undefined” and num < arr[i] even though javascript engine is going to check data type anyway . 您正在检查typeof arr[i] === “undefined”num < arr[i]即使javascript引擎无论如何都要检查数据类型

What I mean is, if it is true that typeof arr[i] === “undefined” , then num < arr[i] will always be false. 我的意思是,如果确实typeof arr[i] === “undefined” ,则num < arr[i]将始终为false。 It would also be false for >= , which leads to the solution. 对于>= ,它也是错误的,这导致了解决方案。

Instead of: 代替:

if (typeof arr[i] === “undefined” || num < arr[i])

You can do: 你可以做:

if (!(num >= arr[i]))

On the other hand... 另一方面...

  • if you don't know that it is currently too slow, which you don't if you did not profile it, then you should not optimize it. 如果您不知道它当前太慢了,如果您没有对其进行分析,则不会这样做,那么您不应该对其进行优化。 See: When is optimisation premature? 请参阅: 什么时候优化过早?

  • why do you even have undefined data in the list to begin with? 为什么你甚至在列表中有未定义的数据? Couldn't you filter that out before writing into the list, rather than at reading time? 你不能在写入列表之前过滤掉它,而不是在阅读时间吗? It is a good idea to always have only one data type in a contaioner (unless you have a really good reason otherwise) 总是在一个内容中只有一种数据类型是个好主意(除非你有一个非常好的理由否则)

  • if you have a key->value mapping with some keys not defined, maybe Array is not the right container for your use case 如果你有一个key->值映射与一些未定义的键,可能Array不是你的用例的正确容器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM