javascript - 减零是某种 JavaScript 性能技巧吗?

标签 javascript jquery

查看 jQuery 核心,我发现了以下代码约定:

nth: function(elem, i, match){
    return match[3] - 0 === i;
},

我真的对片段 match[3] - 0

感到好奇

在 Google 上搜索“-0”的效率并不高,搜索“负零”会返回对鲍勃·迪伦 (Bob Dylan) 歌曲的引用。

所以,谁能告诉我。这是某种性能技巧,还是有这样做的原因而不是 parseIntparseFloat

最佳答案

可能只是强制左侧为整数的一种简写方法。当然,不如调用函数那么清晰。

This tutorial on type-conversion状态:

Any mathematical operator except the concatenation/addition operator will force type-conversion. So conversion of a string to a number might entail performing a mathematical operation on the string representation of the number that would not affect the resulting number, such as subtracting zero or multiplying by one.

这也表明“减去”是比“减”更好的搜索词。 :)

关于javascript - 减零是某种 JavaScript 性能技巧吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2665984/

相关文章:

JavaScript - 通过键/值对将对象添加到对象并删除

javascript - 如何检测鼠标何时停止

javascript - 如何像 vfs_fonts.js 一样将字体文件 ttf 转换为字符串数据?

c# - 如何在悬停时更改图像源?

javascript - 客户端pdf javascript插件,pdf支持多种语言

javascript - 如何标准化 tensorflow.js 中的图像?

javascript - Nodejs5 和 babel 中的 "unexpected token import"?

javascript - Nicedit.js - 无法添加自定义按钮

javascript - 如何从背景 CSS 属性获取多个图像 URL?

javascript - 使用 jQuery 在 div 中随机淡入淡出