Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean vs Int in Javascript

I always assumed that booleans were more efficient than ints at storing an on/off value - considering that's their reason for existence. I recently decided to check if this is true with the help of jsperf, and it came up with some contrary results!

http://jsperf.com/bool-vs-int

Here is the first test I tried. Toggling the value of the on/off switch. On Chrome it's significantly faster to do this using 1/0, but on firefox it's slightly faster to do this using bool. Interesting.

http://jsperf.com/bool-vs-int-2

And here's the second test I tried. Using them in a conditional. This appears to have significant advantage for ints as opposed to bools, up to 70% faster to use 1/0 instead of booleans - on both firefox and chrome. Wtf?

I guess my question is, am I doing something wrong? Why are ints so much better at boolean's job? Is the only value of using bools clarity, or am I missing something important?

like image 748
Thristhart Avatar asked Aug 28 '11 15:08

Thristhart


People also ask

What is the difference between boolean and integer?

A boolean true is, well, a boolean value. Use it whenever you want to express that a certain binary condition is met. The integer literal 1 is a number. Use it, whenever you are counting something.

Should I use bool or int?

If you choose a bool (boolean) type, it is clear there are only two acceptable values: true or false . If you use an int (integer) type, it is no longer clear that the intent of that variable can only be 1 or 0 or whatever values you chose to mean true and false .

What is boolean JavaScript?

In JavaScript, a boolean value is one that can either be TRUE or FALSE. If you need to know “yes” or “no” about something, then you would want to use the boolean function. It sounds extremely simple, but booleans are used all the time in JavaScript programming, and they are extremely useful.

Can a boolean be a number?

A Boolean number has value True/ON and False/OFF; therefore, when a Boolean is converted into a number it would return 1 or 0 for True/ON or False/OFF respectively.


1 Answers

Disclaimer, I can only speak for Firefox, but I guess Chrome is similar.

First example (http://jsperf.com/bool-vs-int):

  1. The Not operation JägerMonkey (Spidmonkey's JavaScript methodjit) inlines the check for boolean first and then just xors, which is really fast (We don't know the type of a/b, so we need to check the type). The second check is for int, so if a/b would be a int this would be a little bit slower. Code

  2. The Subtract operation. We again don't know the type of c/d. And again you are lucky we are going to assume ints and inline that first. But because in JavaScript number operations are specified to be IEEE 754 doubles, we need to check for overflow. So the only difference is "sub" and a "conditional jump" on overflow vs. plain xor in case 1. Code

Second example: (I am not 100% sure about these, because I never really looked at this code before)

  1. and 3. The If. We inline a check for boolean, all other cases end up calling a function converting the value to a boolean. Code

  2. The Compare and If. This one is a really complex case from the implementation point of view, because it was really important to optimize equality operations. So I think I found the right code, that seems to suggest we first check for double and then for integers. And because we know that the result of a compare is always a boolean, we can optimize the if statement. Code

Followup I dumped the generated machine code, so if you are still interested, here you go.

Overall this is just a piece in a bigger picture. If we knew what kind of type the variables had and knew that the subtraction won't overflow then we could make all these cases about equally fast. These efforts are being made with IonMonkey or v8's Crankshaft. This means you should avoid optimizing based of this information, because:

  1. it's already pretty fast
  2. the engine developers take care of optimizing it for you
  3. it will be even faster in the future.
like image 93
evilpie Avatar answered Sep 27 '22 20:09

evilpie