Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

备稿计划: ===== 的区别及使用 #123

Open
iugo opened this issue Oct 19, 2022 · 0 comments
Open

备稿计划: ===== 的区别及使用 #123

iugo opened this issue Oct 19, 2022 · 0 comments

Comments

@iugo
Copy link
Member

iugo commented Oct 19, 2022

一直以来, 我们都在用 === (全等), 而避免使用 == (非全等). 主要原因在于我们想避免 "隐性操作", 比如 == 自带的类型转换.

我们认为, 类型转换必然带来额外消耗, 所以性能上非全等一定是落后的.

但是在看一些注重性能的库时, 仍然看到许多 == 的使用.

然后我们注意到 deno lint https://lint.deno.land/?q=&all=on#eqeqeq 没有将要求全等作为建议规则.

这就让我产生了一丝怀疑, 难道实际执行上 == 性能不差或者有别的什么好处?

首先我们看一下性能:

在网上看到一些例子 https://www.adityabansod.net/2018/09/performance-implications-when-comparing-types-in-node-js/ 中总结到, 非全等会根据环境, 只会比全等更慢.

然后看到 VS Code 项目内有关于此的讨论: microsoft/vscode#99947

里面谈到 == null 可以被看作 isUndefinedOrNull. 但是同时有人反对, 因为容易被看混.

再回到性能层面, == null=== null || === undefined 有性能差异吗? 经过测试我们发现, 几乎没有.

https://www.typescriptlang.org/play?#code/MYewdgzgLgBAsgQQBowLwwIwAYe5wbgChDRIQAbAUwDooBLAW0oAoByKS6DVgSiIDMQAJxjMqsOmkz4YkgDzxkMugGoVPGAG9CMGBy7MwAV3Lk+OvZygZmRsABNK-OmEr3zu-deYYPlg6wAhrxEAL4k4BAUNPRMAKIObF7c5sSkUVS0jCzsVgBMIYSCImKUElIYyjAKiEjKahranvmGJmZEzdB5tg5OLm5+Xt2+Hf5Q3UGF4enRWfGJuV2FRXbA9OBjNgBuAFwwgWAAno0WQmVGQmAwW2joxqZhxPyr61dDzLv7Rye6Z1AXVxuqGBMHu5BgAB8Iddbug7I5nK57GEgA

@iugo iugo changed the title 备稿指南: ===== 的区别 备稿计划: ===== 的区别 Oct 19, 2022
@iugo iugo changed the title 备稿计划: ===== 的区别 备稿计划: ===== 的区别及使用 Oct 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant