什么是JavaScript中的“双波浪号”(~~)运算符? [重复]

双波浪号~~在JavaScript中是一个双重NOT逐位运算符,常用于快速将值转换为整数。它等效于使用Math.floor()或Math.ceil(),但可能在某些情况下速度更快。然而,由于现代浏览器的优化,这种速度差异可能不再显著,代码可读性比微小的性能提升更重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:What is the “double tilde” (~~) operator in JavaScript? [duplicate]

This question already has answers here : 这个问题已经在这里有了答案
Closed 6 years ago . 6年前关闭。

I'm seeing this in some code, and I have no idea what it does: 我在某些代码中看到了这一点,但我不知道它的作用:

var jdn = function(y, m, d) {
  var tmp = (m <= 2 ? -1 : 0);
  return ~~((1461 * (y + 4800 + tmp)) / 4) + 
         ~~((367 * (m - 2 - 12 * tmp)) / 12) - 
         ~~((3 * ((y + 4900 + tmp) / 100)) / 4) + 
         d - 2483620;
};

What's the ~~ operator do? ~~运算符是做什么的?


#1楼

参考:https://stackoom.com/question/P3Ur/什么是JavaScript中的-双波浪号-运算符-重复


#2楼

The diffrence is very simple: 区别很简单:

Long version 长版

If you want to have better readability, use Math.floor . 如果要提高可读性,请使用Math.floor But if you want to minimize it, use tilde ~~ . 但是,如果要将其最小化,请使用波浪号~~

There are a lot of sources on the internet saying Math.floor is faster, but sometimes ~~ . 互联网上有很多消息来源说Math.floor更快,但有时~~ I would not recommend you think about speed because it is not going to be noticed when running the code. 我不建议您考虑速度,因为在运行代码时不会注意到速度。 Maybe in tests etc, but no human can see a diffrence here. 也许正在测试等中,但是没有人可以看到这里的差异。 What would be faster is to use ~~ for a faster load time. 更快的方法是使用~~以加快加载时间。

Short version 简洁版本

~~ is shorter/takes less space. ~~较短/占用较少的空间。 Math.floor improves the readability. Math.floor提高了可读性。 Sometimes tilde is faster, sometimes Math.floor is faster, but it is not noticeable. 有时波浪号速度更快,有时Math.floor速度更快,但是并不明显。


#3楼

That ~~ is a double NOT bitwise operator. ~~是一个双重NOT逐位运算符。

It is used as a faster substitute for Math.floor() . 它用作Math.floor()更快替代品


#4楼

~(5.5)   // => -6
~(-6)    // => 5
~~5.5    // => 5  (same as Math.floor(5.5))
~~(-5.5) // => -5 (NOT the same as Math.floor(-5.5), which would give -6 )

For more info, see: 有关更多信息,请参见:


#5楼

It hides the intention of the code. 它隐藏了代码的意图。

It's two single tilde operators, so it does a bitwise complement (bitwise not) twice. 它是两个单个的波浪号运算符,因此它执行了一次按位补码(按位取反)两次。 The operations take out each other, so the only remaining effect is the conversion that is done before the first operator is applied, ie converting the value to an integer number. 这些操作相互抵消,因此唯一剩下的效果是在应用第一个运算符之前进行的转换,即将值转换为整数。

Some use it as a faster alternative to Math.floor , but the speed difference is not that dramatic, and in most cases it's just micro optimisation. 有些人将其用作Math.floor的更快替代品,但速度差异并不那么显着,并且在大多数情况下只是微优化。 Unless you have a piece of code that really needs to be optimised, you should use code that descibes what it does instead of code that uses a side effect of a non-operation. 除非您有一段确实需要优化的代码,否则您应该使用描述其功能的代码,而不是使用非操作副作用的代码。

Update 2011-08: 2011-08更新:

With optimisation of the JavaScript engine in browsers, the performance for operators and functions change. 通过优化浏览器中的JavaScript引擎,操作员和函数的性能会发生变化。 With current browsers, using ~~ instead of Math.floor is somewhat faster in some browsers, and not faster at all in some browsers. 对于当前的浏览器,在某些浏览器中使用~~代替Math.floor更快,而在某些浏览器中则根本Math.floor If you really need that extra bit of performance, you would need to write different optimised code for each browser. 如果您确实需要额外的性能,则需要为每个浏览器编写不同的优化代码。

See: tilde vs floor 另请: 波浪线与地板

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值