将布尔结果转换为数字/整数

发布于 2024-12-10 21:12:26 字数 103 浏览 0 评论 0原文

我有一个存储 falsetrue 的变量,但我分别需要 01 。我该怎么做?

I have a variable that stores false or true, but I need 0 or 1 instead, respectively. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(21

花想c 2024-12-17 21:12:26

使用 一元 + 运算符,将其操作数转换为数字。

+ true; // 1
+ false; // 0

当然,请注意,您仍然应该在服务器端清理数据,因为用户可以将任何数据发送到您的服务器,无论客户端代码说什么。

Use the unary + operator, which converts its operand into a number.

+ true; // 1
+ false; // 0

Note, of course, that you should still sanitise the data on the server side, because a user can send any data to your sever, no matter what the client-side code says.

飘过的浮云 2024-12-17 21:12:26

Javascript 有一个三元运算符,您可以使用:

var i = result ? 1 : 0;

Javascript has a ternary operator you could use:

var i = result ? 1 : 0;
蔚蓝源自深海 2024-12-17 21:12:26

恕我直言,最好的解决方案是:

fooBar | 0

这在 asm.js 中用于强制整数类型。

Imho the best solution is:

fooBar | 0

This is used in asm.js to force integer type.

太傻旳人生 2024-12-17 21:12:26

我更喜欢使用数字函数。它接受一个对象并将其转换为数字。

示例:

var myFalseBool = false;
var myTrueBool = true;

var myFalseInt = Number(myFalseBool);
console.log(myFalseInt === 0);

var myTrueInt = Number(myTrueBool);
console.log(myTrueInt === 1);

您可以在 jsFiddle 中测试它。

I prefer to use the Number function. It takes an object and converts it to a number.

Example:

var myFalseBool = false;
var myTrueBool = true;

var myFalseInt = Number(myFalseBool);
console.log(myFalseInt === 0);

var myTrueInt = Number(myTrueBool);
console.log(myTrueInt === 1);

You can test it in a jsFiddle.

不必在意 2024-12-17 21:12:26

执行此操作的键入方法是:

Number(true) // 1
Number(false) // 0

The typed way to do this would be:

Number(true) // 1
Number(false) // 0
拿命拼未来 2024-12-17 21:12:26

我创建了所有建议答案的 JSperf 比较。

TL;DR - 所有当前浏览器的最佳选择是:

val | 0;

.

更新:

现在看来它们都非常相同,除了 Number() 函数是最慢的,而最好的是 val === true ? 1:0;

I created a JSperf comparison of all suggested answers.

TL;DR - the best option for all current browsers is:

val | 0;

.

Update:

It seems like these days they are all pretty identical, except that the Number() function is the slowest, while the best being val === true ? 1 : 0;.

你怎么敢 2024-12-17 21:12:26

我今天才发现这条捷径。

~~(true)

~~(false)

人们比我能解释的聪明得多:

http: //james.padolsey.com/javascript/double-bitwise-not/

I just came across this shortcut today.

~~(true)

~~(false)

People much smarter than I can explain:

http://james.padolsey.com/javascript/double-bitwise-not/

飘然心甜 2024-12-17 21:12:26

TL;DR:避免 Number 构造函数和 +bool;默认情况下使用简单的 if;求助于 bool | 0, 1 * bool 如果您的项目中的基准测试通过这种方式表现得更好。

这是一个相当老的问题,并且有很多有效的答案。我注意到,这里的所有基准测试都是无关紧要的 - 没有考虑到 分支 预测。另外,如今,JS 引擎不再简单地解释代码,它们 JIT 将其编译为本机机器代码并在执行之前对其进行优化。这意味着,除了分支预测之外,编译器甚至可以用最终值替换表达式。

现在,这两个因素如何影响布尔到整数转换的性能?让我们来看看吧!在我们进行基准测试之前,了解我们的基准测试内容很重要。对于转换,我们使用以下七种转换方法:

  • Number 构造函数:Number(bool)
  • If 语句(使用三元):bool ? 1 : 0
  • 一元运算符 +: +bool
  • 按位或: bool | 0
  • 按位与:bool & 1
  • 位双非:~~bool
  • 数字乘法:bool * 1

“转换”表示将 false 转换为 0true11。每个转换方法运行 100000 次,测量操作/毫秒。在下表中,转换方法将根据其结果进行相应分组。结果后面的百分比表示在同一浏览器中此方法与最快方法相比有多慢。如果没有百分比,则该方法要么是最快的,要么差异可以忽略不计(<0.​​01%)。基准测试在 Macbook Pro 16 英寸机器上运行,配备 Apple M1 Pro 10 核 CPU 和 16GB RAM。浏览器为 Chrome 102、Firefox 101 和 Safari 15.5。

第一个 基准 转换常量 true

方法Chrome (V8)Firefox ( Spidermonkey)Safari (Webkit)
Number(bool)31745.89392.35 - 91.48%31231.79
<代码>布尔? 1:031592.8 - 0.48%4602.6427533.47 - 11.84%
+bool31332.57 - 1.3%4463.41 - 3.02%27378.7 - 12.34%
bool | 031488.5 - 0.81%3.5%27222 - 12.84%
4441.4 - 131383.17 - 1.14%4459.64 - 3.11%27317.41 - 12.53%
~~布尔31265.85 - 1.51%4442.35 - 3.48%27434.72 - 12.16%
布尔 * 131374.4 - 1.17%4444.05 - 3.45%27381.19 - 12.33%

有趣! V8 显示了一些巨大的数字,所有这些数字都大致相同! Spidermonkey 并没有真正发光,但我们可以看到按位和乘法技巧是第一位的,然后是三元运算技巧。最后,Webkit 的 Number 与 V8 的类似,其他方法落后,但彼此接近。有哪些要点?浏览器大多设法用简单的值 1 替换我们的转换。这种优化将发生在我们可以在心里将布尔值替换为常量值的地方。 Number 构造函数是一个有趣的异常 - 它在 Firefox 中严重落后( 91%!),而在 Safari 中它是最快的!

上述情况不是我们在实际项目中遇到的情况。因此,让我们更改变量:bool 现在是 Math.random() Math.random() Math.random() 0.5。这产生 true 的概率为 50%,false 的概率为 50%。我们的结果会改变吗?让我们运行这个基准测试来看看。

方法Chrome (V8)Firefox (Spidermonkey)Safari (Webkit)
数字(bool)1648.83 - 2.26%280.34 - 86.4%8014.69
bool ? 1:0804.27 - 52.32%731.57 - 64.5%1294.02 - 83.85%
+bool1670.79 - 0.95%2057.947753.99 - 3.25%
bool | 01668.22 - 1.11%2054.177764.81 - 3.12%
布尔值11675.52 - 0.67%2056.767193.08 - 10.25%
~~bool1676.24 - 0.63%2056.187669.48 - 4.31%
bool * 11686.882060.887751.48 - 3.28%

现在结果更加一致。我们在各个浏览器中看到三元 if、按位和乘法方法的数字相似,并且 Number 构造函数在 Firefox 上的表现再次最差。三进制落后了,因为它会产生分支。总体而言,Safari 似乎是我们表现最好的,每种方法都能快速产生结果!

现在让我们看看分支预测如何通过以下基准影响我们的结果,其中我们将布尔变量更改为<代码>Math.random() < 0.01,这意味着 1% true,99% false

方法Chrome (V8)Firefox (Spidermonkey)Safari (Webkit)
Number(bool)1643.13 - 1.68%280.06 - 86.4%8071.65
bool ? 1:01590.55 - 4.83%1970.66 - 4.32%7119.59 - 11.8%
+bool1662.09 - 0.55%2054.097762.03 - 3.84%
bool | 01669.352051.857743.95 - 4.06%
布尔值11661.09 - 0.61%2057.627454.45 - 7.65%
~~bool1662.94 - 0.5%2059.657739.4 - 4.12%
bool * 11671.282048.217787.38 - 3.52%

意外?预期的?我想说后者,因为在这种情况下,分支预测在几乎所有情况下都是成功的,因为三元 if 和按位 hack 之间的差异较小。其他的结果都是一样的,这里就不多说了。我仍然会指出 Number 在 Firefox 中的可怕性能 - 为什么?

这一努力让我们回到了最初的问题:如何在 Javascript 中将 bool 转换为 int?以下是我的建议:

  • 一般来说,使用 if 语句。不要变得聪明——浏览器通常会做得更好,并且通常意味着大多数情况。它们是这里所有方法中最具可读性和最清晰的。当我们处于可读性时,也许可以使用 if (bool) 而不是那个丑陋的三元组!我希望 Javascript 具有 RustPython 有...
  • 在真正必要时使用其余部分。也许您项目中的基准测试执行不合格,并且您发现令人讨厌的 if 会导致性能不佳 - 如果是这种情况,请随时进入 无分支 编程!但不要在那个兔子洞里走得太深,相信我,没有人会从 -1 * (a < b) + 1 * (a > b) 这样的东西中受益。

还有一些细节:

  • 避免 Number(bool)。虽然 Chromium 平台(Chrome + Edge)在全球拥有约 68% 的市场份额,Safari 为 19%,Firefox 仅为 3.6%,但还有足够多的其他快速执行方法,不会完全牺牲一定比例的用户。 Firefox 拥有 7% 的桌面市场份额,相当于 1.73 亿用户。
  • 在较旧的基准测试中,+bool 的表现与 Firefox 中的 Number 的表现类似,也许也考虑到这一点 - 按位黑客和乘法在所有浏览器中给出了一致的性能结果情况。 bool | 0 最有机会被其他开发者熟悉。

编辑:以前的基准测试工具提供了模糊的结果,没有测量单位。我已经改变了它,并且还添加了 Safari 的基准,这影响了结论。


  1. 定义转换是因为尚不清楚布尔值到整数的含义。例如,前往 根本不支持这种转换

TL;DR: Avoid Number constructor and +bool; use a simple if by default; resort to bool | 0, 1 * bool if benchmarks in your project do better this way.

This is quite an old question, and there exist many valid answers. Something I've noticed is that all benchmarks here are irrelevant - none take into account branch prediction. Also, nowadays, JS engines don't simply interpret the code, they JIT compile it to native machine code and optimize it prior to execution. This means that, besides branch prediction, the compiler can even substitute expressions with their final value.

Now, how do these 2 factors affect the performance of, well, boolean to integer conversion? Let's find out! Before we get into the benchmarks, it is important to know what we benchmark. For the conversion, we're using the following seven conversion methods:

  • Number constructor: Number(bool)
  • If statement (ternary used): bool ? 1 : 0
  • Unary operator +: +bool
  • Bitwise OR: bool | 0
  • Bitwise AND: bool & 1
  • Bitwise double NOT: ~~bool
  • Number multiplication: bool * 1

"Conversion" means converting false to 0 and true to 11. Each conversion method is ran 100000 times, measuring operations/millisecond. In the following tables, conversion methods will be grouped to their results accordingly. The percentage after the result represents how slow this method is compared to the fastest, in the same browser. If there is no percentage, the method is either the fastest or the difference is negligible (<0.01%). Benchmarks are run on a Macbook Pro 16-inch machine, with the Apple M1 Pro 10-core CPU and 16GB of RAM. Browsers are Chrome 102, Firefox 101 and Safari 15.5.

The first benchmark converts the constant true:

MethodChrome (V8)Firefox (Spidermonkey)Safari (Webkit)
Number(bool)31745.89392.35 - 91.48%31231.79
bool ? 1 : 031592.8 - 0.48%4602.6427533.47 - 11.84%
+bool31332.57 - 1.3%4463.41 - 3.02%27378.7 - 12.34%
bool | 031488.5 - 0.81%4441.4 - 3.5%27222 - 12.84%
bool & 131383.17 - 1.14%4459.64 - 3.11%27317.41 - 12.53%
~~bool31265.85 - 1.51%4442.35 - 3.48%27434.72 - 12.16%
bool * 131374.4 - 1.17%4444.05 - 3.45%27381.19 - 12.33%

Interesting! V8 shows some huge numbers, all of them approximately the same! Spidermonkey doesn't really shine, but we can see that the bitwise and multiplication tricks come first, and the ternary if second. Finally, Webkit's Number does similarly to V8's and the other methods fall behind, but are all close to each other. What are the takeaways? Browsers mostly manage to replace our conversions with simply the value 1. This optimization will take place where we can mentally replace the boolean to a constant value. The Number constructor is an intriguing anomaly - it severly falls behind in Firefox (91% slower!), while in Safari it is the fastest!

That above isn't a situation we'll ever encounter in real projects. So let's change our variables: the bool is now Math.random() < 0.5. This yields a 50% chance of true, 50% of false. Do our results change? Let's run this benchmark to see.

MethodChrome (V8)Firefox (Spidermonkey)Safari (Webkit)
Number(bool)1648.83 - 2.26%280.34 - 86.4%8014.69
bool ? 1 : 0804.27 - 52.32%731.57 - 64.5%1294.02 - 83.85%
+bool1670.79 - 0.95%2057.947753.99 - 3.25%
bool | 01668.22 - 1.11%2054.177764.81 - 3.12%
bool & 11675.52 - 0.67%2056.767193.08 - 10.25%
~~bool1676.24 - 0.63%2056.187669.48 - 4.31%
bool * 11686.882060.887751.48 - 3.28%

The results are more consistent now. We see similar numbers for ternary if, bitwise, and multiplication methods across browsers, and the Number constructor again performs the worst on Firefox. Ternary falls behind, as it generates branches. Safari seems to be our top performer overall, each method yielding blazing fast results!

Let's see now how branch prediction affects our results with the following benchmark, where we change our boolean variable to Math.random() < 0.01, which means 1% true, 99% false.

MethodChrome (V8)Firefox (Spidermonkey)Safari (Webkit)
Number(bool)1643.13 - 1.68%280.06 - 86.4%8071.65
bool ? 1 : 01590.55 - 4.83%1970.66 - 4.32%7119.59 - 11.8%
+bool1662.09 - 0.55%2054.097762.03 - 3.84%
bool | 01669.352051.857743.95 - 4.06%
bool & 11661.09 - 0.61%2057.627454.45 - 7.65%
~~bool1662.94 - 0.5%2059.657739.4 - 4.12%
bool * 11671.282048.217787.38 - 3.52%

Unexpected? Expected? I'd say the latter, because in this case branch prediction was successful in almost all cases, given the smaller difference between the ternary if and bitwise hacks. All other results are the same, not much else to say here. I'll still point out the horrific performance of Number in Firefox - why?

This endeavour brings us back to the original question: how to convert bool to int in Javascript? Here are my suggestions:

  • Use if statements, in general. Don't get smart - the browser will do better, usually, and usually means most of the situations. They are the most readable and clear out of all the methods here. While we're at readability, maybe use if (bool) instead of that ugly ternary! I wish Javascript had what Rust or Python have...
  • Use the rest when it's truly necessary. Maybe benchmarks in your project perform sub-standard, and you found that a nasty if causes bad performance - if that's the case, feel free to get into branchless programming! But don't go too deep in that rabbit hole, nobody will benefit from things like -1 * (a < b) + 1 * (a > b), believe me.

And some specifics:

  • Avoid Number(bool). While it is true that the Chromium platform (Chrome + Edge) has about 68% market share globally, Safari 19% and Firefox a mere 3.6%, there are enough other fast-performing methods that won't fully sacrifice a percentage of your users. Firefox has 7% desktop market share, which amounts to a sizable number of 173 million users.
  • In older benchmarks, +bool performed similarly bad to Number in Firefox, maybe take this in consideration, too - bitwise hacks and multiplication give consistently performant results across all browsers, in all situations. bool | 0 has the best chance to be familiar to other developers.

EDIT: The previous benchmarking tool provided vague results, without an unit of measure. I've changed it, and also added benchmarks for Safari, which have influenced the conclusions.


  1. Defined the conversion because it's not truly clear what boolean to integer means. For example, Go does not support this conversion at all.
一紙繁鸢 2024-12-17 21:12:26

当 JavaScript 需要数字值但收到布尔值时,它会将该布尔值转换为数字:true 和 false 分别转换为 1 和 0。所以你可以利用这一点;

var t = true;
var f = false;

console.log(t*1); // t*1 === 1
console.log(f*1); // f*1 === 0 

console.log(+t); // 0+t === 1 or shortened to +t === 1
console.log(+f); //0+f === 0 or shortened to +f === 0

进一步阅读《Javascript 权威指南》的类型转换第 3.8 章。

When JavaScript is expecting a number value but receives a boolean instead it converts that boolean into a number: true and false convert into 1 and 0 respectively. So you can take advantage of this;

var t = true;
var f = false;

console.log(t*1); // t*1 === 1
console.log(f*1); // f*1 === 0 

console.log(+t); // 0+t === 1 or shortened to +t === 1
console.log(+f); //0+f === 0 or shortened to +f === 0

Further reading Type Conversions Chapter 3.8 of The Definitive Guide to Javascript.

围归者 2024-12-17 21:12:26

我只是在编写的一些代码中处理这个问题。我的解决方案是使用按位与。

var j = bool & 1;

处理持续问题的更快方法是创建一个函数。它更容易被其他人阅读,在维护阶段更好地理解,并且消除了写错东西的可能性。

function toInt( val ) {
    return val & 1;
}

var j = toInt(bool);

编辑 - 2014 年 9 月 10 日

由于某种原因,在 Chrome 中使用三元运算符与相同运算符进行转换的速度并不快。没有任何意义为什么它更快,但我认为这是某种低级优化,在整个过程中的某个地方有意义。

var j = boolValue === true ? 1 : 0;

自己测试一下:http://jsperf.com/boolean-int-conversion/2

在 FireFox 和 Internet Explorer 中,使用我发布的版本通常速度更快。

编辑 - 2017 年 7 月 14 日

好吧,我不会告诉您应该或不应该使用哪一个。每个奇怪的浏览器在使用每种方法执行操作的速度上都在上下波动。 Chrome 曾经一度拥有按位 & 功能。版本比其他版本做得更好,但随后突然变得更糟。我不知道他们在做什么,所以我就把它留给在乎的人。很少有任何理由去关心这样的操作完成的速度有多快。即使在移动设备上,这也是一个无操作。

另外,这里有一个更新的方法,用于添加不能被覆盖的“toInt”原型。

Object.defineProperty(Boolean.prototype, "toInt", { value: function()
{
    return this & 1;
}});

I was just dealing with this issue in some code I was writing. My solution was to use a bitwise and.

var j = bool & 1;

A quicker way to deal with a constant problem would be to create a function. It's more readable by other people, better for understanding at the maintenance stage, and gets rid of the potential for writing something wrong.

function toInt( val ) {
    return val & 1;
}

var j = toInt(bool);

Edit - September 10th, 2014

No conversion using a ternary operator with the identical to operator is faster in Chrome for some reason. Makes no sense as to why it's faster, but I suppose it's some sort of low level optimization that makes sense somewhere along the way.

var j = boolValue === true ? 1 : 0;

Test for yourself: http://jsperf.com/boolean-int-conversion/2

In FireFox and Internet Explorer, using the version I posted is faster generally.

Edit - July 14th, 2017

Okay, I'm not going to tell you which one you should or shouldn't use. Every freaking browser has been going up and down in how fast they can do the operation with each method. Chrome at one point actually had the bitwise & version doing better than the others, but then it suddenly was much worse. I don't know what they're doing, so I'm just going to leave it at who cares. There's rarely any reason to care about how fast an operation like this is done. Even on mobile it's a nothing operation.

Also, here's a newer method for adding a 'toInt' prototype that cannot be overwritten.

Object.defineProperty(Boolean.prototype, "toInt", { value: function()
{
    return this & 1;
}});
度的依靠╰つ 2024-12-17 21:12:26

一元 + 运算符会处理这个问题:

var test = true;
// +test === 1
test = false;
// +test === 0

在存储它之前,您自然会希望在服务器上对其进行健全性检查,因此无论如何,这可能是一个更明智的地方。

The unary + operator will take care of this:

var test = true;
// +test === 1
test = false;
// +test === 0

You'll naturally want to sanity-check this on the server before storing it, so that might be a more sensible place to do this anyway, though.

雪花飘飘的天空 2024-12-17 21:12:26

您还可以添加 0、使用移位运算符或异或:

val + 0;
val ^ 0;
val >> 0;
val >>> 0;
val << 0;

它们的速度与其他答案相似。

You can also add 0, use shift operators or xor:

val + 0;
val ^ 0;
val >> 0;
val >>> 0;
val << 0;

These have similar speeds as those from the others answers.

简美 2024-12-17 21:12:26

在我的上下文中,React Native 中我从布尔值获取不透明度值,最简单的方法是:使用一元 + 运算符。

+ true; // 1
+ false; // 0

这会将布尔值转换为数字;

style={ opacity: +!isFirstStep() }

In my context, React Native where I am getting opacity value from boolean, the easiest way: Use unary + operator.

+ true; // 1
+ false; // 0

This converts the boolean into number;

style={ opacity: +!isFirstStep() }
尾戒 2024-12-17 21:12:26

+!! 允许您将其应用于变量,即使它是未定义

+!!undefined    // 0
+!!false        // 0
+!!true         // 1

+!!(<boolean expression>)  // 1 if it evaluates to true, 0 otherwise

+!! allows you to apply this on a variable even when it's undefined:

+!!undefined    // 0
+!!false        // 0
+!!true         // 1

+!!(<boolean expression>)  // 1 if it evaluates to true, 0 otherwise
千笙结 2024-12-17 21:12:26

您可以通过简单地扩展布尔原型来做到这一点

Boolean.prototype.intval = function(){return ~~this}

理解那里发生的事情并不是太容易,因此替代版本将

Boolean.prototype.intval = function(){return (this == true)?1:0}

完成,您可以做类似的事情

document.write(true.intval());

当我使用布尔值来存储条件时我经常将它们转换为位字段在这种情况下,我最终使用原型函数的扩展版本

Boolean.prototype.intval = function(places)
{
 places = ('undefined' == typeof(places))?0:places; 
 return (~~this) << places
}

,您可以使用

document.write(true.intval(2))

它生成 4 作为其输出。

You could do this by simply extending the boolean prototype

Boolean.prototype.intval = function(){return ~~this}

It is not too easy to understand what is going on there so an alternate version would be

Boolean.prototype.intval = function(){return (this == true)?1:0}

having done which you can do stuff like

document.write(true.intval());

When I use booleans to store conditions I often convert them to bitfields in which case I end up using an extended version of the prototype function

Boolean.prototype.intval = function(places)
{
 places = ('undefined' == typeof(places))?0:places; 
 return (~~this) << places
}

with which you can do

document.write(true.intval(2))

which produces 4 as its output.

眼睛会笑 2024-12-17 21:12:26
let integerVariable = booleanVariable * 1;
let integerVariable = booleanVariable * 1;
帅气尐潴 2024-12-17 21:12:26

尝试

val*1

let t=true;
let f=false;

console.log(t*1);
console.log(f*1)

try

val*1

let t=true;
let f=false;

console.log(t*1);
console.log(f*1)

稳稳的幸福 2024-12-17 21:12:26

我已经测试了所有这些示例,我做了一个基准测试,最后我建议您选择较短的一个,它不会影响性能。

在 Ubuntu 服务器 14.04、nodejs v8.12.0 - 26/10/18 中运行

    let i = 0;
console.time("TRUE test1")
    i=0;
    for(;i<100000000;i=i+1){
        true ? 1 : 0;
    }
console.timeEnd("TRUE test1")


console.time("FALSE test2")
    i=0;
    for(;i<100000000;i=i+1){
        false ? 1 : 0;
    }
console.timeEnd("FALSE test2")

console.log("----------------------------")

console.time("TRUE test1.1")
    i=0;
    for(;i<100000000;i=i+1){
        true === true ? 1 : 0;
    }
console.timeEnd("TRUE test1.1")


console.time("FALSE test2.1")
    i=0;
    for(;i<100000000;i=i+1){
        false === true ? 1 : 0;
    }
console.timeEnd("FALSE test2.1")

console.log("----------------------------")

console.time("TRUE test3")
    i=0;
    for(;i<100000000;i=i+1){
        true | 0;
    }
console.timeEnd("TRUE test3")

console.time("FALSE test4")
    i=0;
    for(;i<100000000;i=i+1){
        false | 0;
    }
console.timeEnd("FALSE test4")

console.log("----------------------------")

console.time("TRUE test5")
    i=0;
    for(;i<100000000;i=i+1){
        true * 1;
    }
console.timeEnd("TRUE test5")

console.time("FALSE test6")
    i=0;
    for(;i<100000000;i=i+1){
        false * 1;
    }
console.timeEnd("FALSE test6")

console.log("----------------------------")

console.time("TRUE test7")
    i=0;
    for(;i<100000000;i=i+1){
        true & 1;
    }
console.timeEnd("TRUE test7")

console.time("FALSE test8")
    i=0;
    for(;i<100000000;i=i+1){
        false & 1;
    }
console.timeEnd("FALSE test8")

console.log("----------------------------")

console.time("TRUE test9")
    i=0;
    for(;i<100000000;i=i+1){
        +true;
    }
console.timeEnd("TRUE test9")

console.time("FALSE test10")
    i=0;
    for(;i<100000000;i=i+1){
        +false;
    }
console.timeEnd("FALSE test10")

console.log("----------------------------")

console.time("TRUE test9.1")
    i=0;
    for(;i<100000000;i=i+1){
        0+true;
    }
console.timeEnd("TRUE test9.1")

console.time("FALSE test10.1")
    i=0;
    for(;i<100000000;i=i+1){
        0+false;
    }
console.timeEnd("FALSE test10.1")

console.log("----------------------------")

console.time("TRUE test9.2")
    i=0;
    for(;i<100000000;i=i+1){
        -true*-1;
    }
console.timeEnd("TRUE test9.2")

console.time("FALSE test10.2")
    i=0;
    for(;i<100000000;i=i+1){
        -false*-1;
    }
console.timeEnd("FALSE test10.2")

console.log("----------------------------")

console.time("TRUE test9.3")
    i=0;
    for(;i<100000000;i=i+1){
        true-0;
    }
console.timeEnd("TRUE test9.3")

console.time("FALSE test10.3")
    i=0;
    for(;i<100000000;i=i+1){
        false-0;
    }
console.timeEnd("FALSE test10.3")

console.log("----------------------------")

console.time("TRUE test11")
    i=0;
    for(;i<100000000;i=i+1){
        Number(true);
    }
console.timeEnd("TRUE test11")

console.time("FALSE test12")
    i=0;
    for(;i<100000000;i=i+1){
        Number(false);
    }
console.timeEnd("FALSE test12")

console.log("----------------------------")

console.time("TRUE test13")
    i=0;
    for(;i<100000000;i=i+1){
        true + 0;
    }
console.timeEnd("TRUE test13")

console.time("FALSE test14")
    i=0;
    for(;i<100000000;i=i+1){
        false + 0;
    }
console.timeEnd("FALSE test14")

console.log("----------------------------")

console.time("TRUE test15")
    i=0;
    for(;i<100000000;i=i+1){
        true ^ 0;
    }
console.timeEnd("TRUE test15")

console.time("FALSE test16")
    i=0;
    for(;i<100000000;i=i+1){
        false ^ 0;
    }
console.timeEnd("FALSE test16")

console.log("----------------------------")

console.time("TRUE test17")
    i=0;
    for(;i<100000000;i=i+1){
        true ^ 0;
    }
console.timeEnd("TRUE test17")

console.time("FALSE test18")
    i=0;
    for(;i<100000000;i=i+1){
        false ^ 0;
    }
console.timeEnd("FALSE test18")

console.log("----------------------------")

console.time("TRUE test19")
    i=0;
    for(;i<100000000;i=i+1){
        true >> 0;
    }
console.timeEnd("TRUE test19")

console.time("FALSE test20")
    i=0;
    for(;i<100000000;i=i+1){
        false >> 0;
    }
console.timeEnd("FALSE test20")

console.log("----------------------------")

console.time("TRUE test21")
    i=0;
    for(;i<100000000;i=i+1){
        true >>> 0;
    }
console.timeEnd("TRUE test21")

console.time("FALSE test22")
    i=0;
    for(;i<100000000;i=i+1){
        false >>> 0;
    }
console.timeEnd("FALSE test22")

console.log("----------------------------")

console.time("TRUE test23")
    i=0;
    for(;i<100000000;i=i+1){
        true << 0;
    }
console.timeEnd("TRUE test23")

console.time("FALSE test24")
    i=0;
    for(;i<100000000;i=i+1){
        false << 0;
    }
console.timeEnd("FALSE test24")

console.log("----------------------------")

console.time("TRUE test25")
    i=0;
    for(;i<100000000;i=i+1){
        ~~true;
    }
console.timeEnd("TRUE test25")

console.time("FALSE test26")
    i=0;
    for(;i<100000000;i=i+1){
        ~~false;
    }
console.timeEnd("FALSE test26")

console.log("----------------------------")

console.time("TRUE test25.1")
    i=0;
    for(;i<100000000;i=i+1){
        ~true*-1-1;
    }
console.timeEnd("TRUE test25.1")

console.time("FALSE test26.1")
    i=0;
    for(;i<100000000;i=i+1){
        ~false*-1-1;
    }
console.timeEnd("FALSE test26.1")

console.log("----------------------------")

console.time("TRUE test27")
    i=0;
    for(;i<100000000;i=i+1){
        true/1;
    }
console.timeEnd("TRUE test27")

console.time("FALSE test28")
    i=0;
    for(;i<100000000;i=i+1){
        false/1;
    }
console.timeEnd("FALSE test28")

结果

TRUE test1: 93.301ms
FALSE test2: 102.854ms
----------------------------
TRUE test1.1: 118.979ms
FALSE test2.1: 119.061ms
----------------------------
TRUE test3: 97.265ms
FALSE test4: 108.389ms
----------------------------
TRUE test5: 85.854ms
FALSE test6: 87.449ms
----------------------------
TRUE test7: 83.126ms
FALSE test8: 84.992ms
----------------------------
TRUE test9: 99.683ms
FALSE test10: 87.080ms
----------------------------
TRUE test9.1: 85.587ms
FALSE test10.1: 86.050ms
----------------------------
TRUE test9.2: 85.883ms
FALSE test10.2: 89.066ms
----------------------------
TRUE test9.3: 86.722ms
FALSE test10.3: 85.187ms
----------------------------
TRUE test11: 86.245ms
FALSE test12: 85.808ms
----------------------------
TRUE test13: 84.192ms
FALSE test14: 84.173ms
----------------------------
TRUE test15: 81.575ms
FALSE test16: 81.699ms
----------------------------
TRUE test17: 81.979ms
FALSE test18: 81.599ms
----------------------------
TRUE test19: 81.578ms
FALSE test20: 81.452ms
----------------------------
TRUE test21: 115.886ms
FALSE test22: 88.935ms
----------------------------
TRUE test23: 82.077ms
FALSE test24: 81.822ms
----------------------------
TRUE test25: 81.904ms
FALSE test26: 82.371ms
----------------------------
TRUE test25.1: 82.319ms
FALSE test26.1: 96.648ms
----------------------------
TRUE test27: 89.943ms
FALSE test28: 83.646ms

I have tested all of this examples, I did a benchmark, and finally I recommend you choose the shorter one, it doesn't affect in performance.

Runned in Ubuntu server 14.04, nodejs v8.12.0 - 26/10/18

    let i = 0;
console.time("TRUE test1")
    i=0;
    for(;i<100000000;i=i+1){
        true ? 1 : 0;
    }
console.timeEnd("TRUE test1")


console.time("FALSE test2")
    i=0;
    for(;i<100000000;i=i+1){
        false ? 1 : 0;
    }
console.timeEnd("FALSE test2")

console.log("----------------------------")

console.time("TRUE test1.1")
    i=0;
    for(;i<100000000;i=i+1){
        true === true ? 1 : 0;
    }
console.timeEnd("TRUE test1.1")


console.time("FALSE test2.1")
    i=0;
    for(;i<100000000;i=i+1){
        false === true ? 1 : 0;
    }
console.timeEnd("FALSE test2.1")

console.log("----------------------------")

console.time("TRUE test3")
    i=0;
    for(;i<100000000;i=i+1){
        true | 0;
    }
console.timeEnd("TRUE test3")

console.time("FALSE test4")
    i=0;
    for(;i<100000000;i=i+1){
        false | 0;
    }
console.timeEnd("FALSE test4")

console.log("----------------------------")

console.time("TRUE test5")
    i=0;
    for(;i<100000000;i=i+1){
        true * 1;
    }
console.timeEnd("TRUE test5")

console.time("FALSE test6")
    i=0;
    for(;i<100000000;i=i+1){
        false * 1;
    }
console.timeEnd("FALSE test6")

console.log("----------------------------")

console.time("TRUE test7")
    i=0;
    for(;i<100000000;i=i+1){
        true & 1;
    }
console.timeEnd("TRUE test7")

console.time("FALSE test8")
    i=0;
    for(;i<100000000;i=i+1){
        false & 1;
    }
console.timeEnd("FALSE test8")

console.log("----------------------------")

console.time("TRUE test9")
    i=0;
    for(;i<100000000;i=i+1){
        +true;
    }
console.timeEnd("TRUE test9")

console.time("FALSE test10")
    i=0;
    for(;i<100000000;i=i+1){
        +false;
    }
console.timeEnd("FALSE test10")

console.log("----------------------------")

console.time("TRUE test9.1")
    i=0;
    for(;i<100000000;i=i+1){
        0+true;
    }
console.timeEnd("TRUE test9.1")

console.time("FALSE test10.1")
    i=0;
    for(;i<100000000;i=i+1){
        0+false;
    }
console.timeEnd("FALSE test10.1")

console.log("----------------------------")

console.time("TRUE test9.2")
    i=0;
    for(;i<100000000;i=i+1){
        -true*-1;
    }
console.timeEnd("TRUE test9.2")

console.time("FALSE test10.2")
    i=0;
    for(;i<100000000;i=i+1){
        -false*-1;
    }
console.timeEnd("FALSE test10.2")

console.log("----------------------------")

console.time("TRUE test9.3")
    i=0;
    for(;i<100000000;i=i+1){
        true-0;
    }
console.timeEnd("TRUE test9.3")

console.time("FALSE test10.3")
    i=0;
    for(;i<100000000;i=i+1){
        false-0;
    }
console.timeEnd("FALSE test10.3")

console.log("----------------------------")

console.time("TRUE test11")
    i=0;
    for(;i<100000000;i=i+1){
        Number(true);
    }
console.timeEnd("TRUE test11")

console.time("FALSE test12")
    i=0;
    for(;i<100000000;i=i+1){
        Number(false);
    }
console.timeEnd("FALSE test12")

console.log("----------------------------")

console.time("TRUE test13")
    i=0;
    for(;i<100000000;i=i+1){
        true + 0;
    }
console.timeEnd("TRUE test13")

console.time("FALSE test14")
    i=0;
    for(;i<100000000;i=i+1){
        false + 0;
    }
console.timeEnd("FALSE test14")

console.log("----------------------------")

console.time("TRUE test15")
    i=0;
    for(;i<100000000;i=i+1){
        true ^ 0;
    }
console.timeEnd("TRUE test15")

console.time("FALSE test16")
    i=0;
    for(;i<100000000;i=i+1){
        false ^ 0;
    }
console.timeEnd("FALSE test16")

console.log("----------------------------")

console.time("TRUE test17")
    i=0;
    for(;i<100000000;i=i+1){
        true ^ 0;
    }
console.timeEnd("TRUE test17")

console.time("FALSE test18")
    i=0;
    for(;i<100000000;i=i+1){
        false ^ 0;
    }
console.timeEnd("FALSE test18")

console.log("----------------------------")

console.time("TRUE test19")
    i=0;
    for(;i<100000000;i=i+1){
        true >> 0;
    }
console.timeEnd("TRUE test19")

console.time("FALSE test20")
    i=0;
    for(;i<100000000;i=i+1){
        false >> 0;
    }
console.timeEnd("FALSE test20")

console.log("----------------------------")

console.time("TRUE test21")
    i=0;
    for(;i<100000000;i=i+1){
        true >>> 0;
    }
console.timeEnd("TRUE test21")

console.time("FALSE test22")
    i=0;
    for(;i<100000000;i=i+1){
        false >>> 0;
    }
console.timeEnd("FALSE test22")

console.log("----------------------------")

console.time("TRUE test23")
    i=0;
    for(;i<100000000;i=i+1){
        true << 0;
    }
console.timeEnd("TRUE test23")

console.time("FALSE test24")
    i=0;
    for(;i<100000000;i=i+1){
        false << 0;
    }
console.timeEnd("FALSE test24")

console.log("----------------------------")

console.time("TRUE test25")
    i=0;
    for(;i<100000000;i=i+1){
        ~~true;
    }
console.timeEnd("TRUE test25")

console.time("FALSE test26")
    i=0;
    for(;i<100000000;i=i+1){
        ~~false;
    }
console.timeEnd("FALSE test26")

console.log("----------------------------")

console.time("TRUE test25.1")
    i=0;
    for(;i<100000000;i=i+1){
        ~true*-1-1;
    }
console.timeEnd("TRUE test25.1")

console.time("FALSE test26.1")
    i=0;
    for(;i<100000000;i=i+1){
        ~false*-1-1;
    }
console.timeEnd("FALSE test26.1")

console.log("----------------------------")

console.time("TRUE test27")
    i=0;
    for(;i<100000000;i=i+1){
        true/1;
    }
console.timeEnd("TRUE test27")

console.time("FALSE test28")
    i=0;
    for(;i<100000000;i=i+1){
        false/1;
    }
console.timeEnd("FALSE test28")

Result

TRUE test1: 93.301ms
FALSE test2: 102.854ms
----------------------------
TRUE test1.1: 118.979ms
FALSE test2.1: 119.061ms
----------------------------
TRUE test3: 97.265ms
FALSE test4: 108.389ms
----------------------------
TRUE test5: 85.854ms
FALSE test6: 87.449ms
----------------------------
TRUE test7: 83.126ms
FALSE test8: 84.992ms
----------------------------
TRUE test9: 99.683ms
FALSE test10: 87.080ms
----------------------------
TRUE test9.1: 85.587ms
FALSE test10.1: 86.050ms
----------------------------
TRUE test9.2: 85.883ms
FALSE test10.2: 89.066ms
----------------------------
TRUE test9.3: 86.722ms
FALSE test10.3: 85.187ms
----------------------------
TRUE test11: 86.245ms
FALSE test12: 85.808ms
----------------------------
TRUE test13: 84.192ms
FALSE test14: 84.173ms
----------------------------
TRUE test15: 81.575ms
FALSE test16: 81.699ms
----------------------------
TRUE test17: 81.979ms
FALSE test18: 81.599ms
----------------------------
TRUE test19: 81.578ms
FALSE test20: 81.452ms
----------------------------
TRUE test21: 115.886ms
FALSE test22: 88.935ms
----------------------------
TRUE test23: 82.077ms
FALSE test24: 81.822ms
----------------------------
TRUE test25: 81.904ms
FALSE test26: 82.371ms
----------------------------
TRUE test25.1: 82.319ms
FALSE test26.1: 96.648ms
----------------------------
TRUE test27: 89.943ms
FALSE test28: 83.646ms
飞烟轻若梦 2024-12-17 21:12:26

将建议的方法放在 jsben.ch 中:
https://jsben.ch/d33N1

每次测试都会给出不同的结果,但每次测试中最好的方法是按位运算:0|bool / bool|0 / 1&bool / 1&bool / ~~bool

Put suggested methods in jsben.ch:
https://jsben.ch/d33N1.

It gives different results every test, but the best methods in every test are bitwise operations: 0|bool / bool|0 / 1&bool / 1&bool / ~~bool.

猫九 2024-12-17 21:12:26

所有浏览器都支持,支持输入为布尔值或布尔值的字符串表示形式

var yourVarAsStringOrBoolean; 
yourVarAsStringOrBoolean = "true";   //1
yourVarAsStringOrBoolean = "True";   //1
yourVarAsStringOrBoolean = "false";  //0
yourVarAsStringOrBoolean = false;    //0

var resultAsInterger = Number(JSON.parse(yourVarAsStringOrBoolean.toString().toLowerCase()));

使用 Chrome 控制台检查它,它可以工作

Number(JSON.parse(false.toString().toLowerCase()));
Number(JSON.parse("TRUE".toString().toLowerCase()));

supported in all browsers, supports input as boolean or as a string representation of a boolean

var yourVarAsStringOrBoolean; 
yourVarAsStringOrBoolean = "true";   //1
yourVarAsStringOrBoolean = "True";   //1
yourVarAsStringOrBoolean = "false";  //0
yourVarAsStringOrBoolean = false;    //0

var resultAsInterger = Number(JSON.parse(yourVarAsStringOrBoolean.toString().toLowerCase()));

Use Chrome console to check it, it works

Number(JSON.parse(false.toString().toLowerCase()));
Number(JSON.parse("TRUE".toString().toLowerCase()));
生寂 2024-12-17 21:12:26

我知道我这样做有点晚了,但是您可以使用所谓的。取模运算符在 JS 中用百分号 (%) 字符表示。它的目标是给出剩余部分。因此,如果我们将数字除以 2%,它会查找任何能被 2 整除的东西。如果不是,那么余数为 1,情况总是如此,所以你知道这很奇怪。在这种情况下,您要做的就是首先为 integer 提供您想要的任何数字,在这种情况下,您需要 01 >。

const integer = 0;

如果插入1,它将打印false,而0将打印true

const isEven = (integer % 2) === 0;

if (isEven) {
    console.log('true');
} else {
    console.log('false');
} 

我希望这有帮助。

I know I'm a little late to do this, but you can use what's called a modulo. The modulo operator is represented by the percent (%) character in JS. Its goal is to give the remainder. So if we were to divide the number by 2%, it would look for anything divisible than 2. If it isn't, then you have a remainder of 1, which will always be the case, so you know it's an odd. In this case, what you're going to do is first give the integer any number you desire, in which case you want a 0 or 1.

const integer = 0;

If you insert 1 it will print false while 0 will print true.

const isEven = (integer % 2) === 0;

if (isEven) {
    console.log('true');
} else {
    console.log('false');
} 

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文