Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6368689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:42:28+00:00 2026-05-25T00:42:28+00:00

I always assumed that booleans were more efficient than ints at storing an on/off

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:42:28+00:00Added an answer on May 25, 2026 at 12:42 am

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason I have always assumed that most of the time a variable
In pre-.NET world I always assumed that int is faster than byte since this
I always assumed that changing the value of k from x to 20 would
I've always assumed that DbNull.value was a singleton. And thus you could do things
For some reason, I've always assumed that readonly fields have overhead associated with them,
Why do IDE's complain about leaking this in constructor? I've always assumed that it's
I always read that one reason to chose a RESTful architecture is (among others)
I had always assumed that AJAX-driven content was invisible to search engines. (i.e. content
I read somewhere (and have observed) that starting threads is slow. I always assumed
Coming from a C background, I've always assumed the POD types (eg ints) were

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.