最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

performance - Why is Boolean() so slow in Javascript? - Stack Overflow

programmeradmin2浏览0评论

According to the ECMAScript specification, both the unary logical NOT operator (!) and the Boolean() function use the internal function ToBoolean(), and the NOT operator also does a few checks to reverse the result. So why is a double logical NOT operation much faster than running the Boolean() function?

I used the following piece of code to test which was faster:

function logicalNotOperator() {
  var start = performance.now();
  for (var i = 0; i < 9999999; i++) !!Math.random();
  return 0.001 * (performance.now() - start);
}
 
function booleanFunc() {
  var start = performance.now();
  for (var i = 0; i < 9999999; i++) Boolean(Math.random());
  return 0.001 * (performance.now() - start);
}

var logicalNotOperatorResult = logicalNotOperator();
var booleanFuncResult = booleanFunc();
var diff = booleanFuncResult - logicalNotOperatorResult;

console.log('logicalNotOperator:', logicalNotOperatorResult);
console.log('booleanFunc:', booleanFuncResult);
console.log('diff:', diff);

According to the ECMAScript specification, both the unary logical NOT operator (!) and the Boolean() function use the internal function ToBoolean(), and the NOT operator also does a few checks to reverse the result. So why is a double logical NOT operation much faster than running the Boolean() function?

I used the following piece of code to test which was faster:

function logicalNotOperator() {
  var start = performance.now();
  for (var i = 0; i < 9999999; i++) !!Math.random();
  return 0.001 * (performance.now() - start);
}
 
function booleanFunc() {
  var start = performance.now();
  for (var i = 0; i < 9999999; i++) Boolean(Math.random());
  return 0.001 * (performance.now() - start);
}

var logicalNotOperatorResult = logicalNotOperator();
var booleanFuncResult = booleanFunc();
var diff = booleanFuncResult - logicalNotOperatorResult;

console.log('logicalNotOperator:', logicalNotOperatorResult);
console.log('booleanFunc:', booleanFuncResult);
console.log('diff:', diff);

Note: I am not referring to the new Boolean() constructor, but the Boolean() function that coerces the argument it's given to a boolean.

Share Improve this question edited Sep 27, 2018 at 7:06 Valentin Podkamennyi 7,3694 gold badges31 silver badges44 bronze badges asked Mar 11, 2013 at 9:48 Qantas 94 HeavyQantas 94 Heavy 16k31 gold badges72 silver badges88 bronze badges 5
  • "much faster" - some proof? – dfsq Commented Mar 11, 2013 at 9:51
  • Boolean() or new Boolean()? – StarPinkER Commented Mar 11, 2013 at 9:52
  • 1 Boolean() has to go through the bells and whistles of creating a new execution context for each call, where as !!true doesn't; I would guess that this is where a lot of the time is spent. – Matt Commented Mar 11, 2013 at 9:54
  • Sorry, Boolean() was what I mean, of course not new Boolean. – Qantas 94 Heavy Commented Mar 11, 2013 at 10:03
  • 1 It appears like Boolean() has bee faster than !!. Tried in Chrome 86 on two different devices. – Andriy Buday Commented Nov 20, 2020 at 20:31
Add a ment  | 

2 Answers 2

Reset to default 7

While Boolean will call the function (internally optimized), most JITs will inline the double not to use XOR which is far faster (source code reference - JägerMonkey).

And the JSperf: http://jsperf./bool-vs-doublenot

I don't know how Javascript JIT piler executed internally. Also right now the Boolean function works faster in Chrome at 2020. But if there is some different browsers, different versions or different JS engines !! operator works faster I think I know the answer reason why. When you call a function there is extra work inside memory for push stack and pop stack. When you use ! (NOT operator) there is no need to create extra work inside memory for push/pop stack. That is why NOT operator works faster.

发布评论

评论列表(0)

  1. 暂无评论