Amy McBride Amy McBride - 5 months ago 25
Javascript Question

Benefits of hexacimal vs decimal integers in JavaScript

I am looking to write an application which should be able to scale well under load, particularly with regard to network traffic (sort of like a pretty looking version of the old BBS systems, with some live chat facility, and eventually the ability to use live video -- but I'm not thinking about the live video part yet, that's just something on the specifications to implement when we've got the other stuff running properly).

Because of this I need everything to be absolutely as efficient as possible, so it can scale to accommodate the largest possible number of users, with the smallest possible investment (I could probably count the amount of 'spare' money here on the fingers of a dismembered limb).

SO... I'll be asking quite a few efficiency related questions here. The first one being:

When dealing with JavaScript integers are there any efficiency savings to saving them as hexadecimal rather than decimal numbers, or vice-versa. If I have, say:

n = 1375;


and I instead write:

n = 0x55F;


Does it make any particular difference, other than convenience if I was working with grids or systems which by nature are more similar to one or another format?

Or, to put it another way, is there a fundamental difference in the way the JavaScript interpreter is likely to handle hexadecimal vs decimal integers at runtime? Does it perform any implicit conversation between these two types, for example?

Answer

Or, to put it another way, is there a fundamental difference in the way the JavaScript interpreter is likely to handle hexadecimal vs decimal integers at runtime?

No. Numbers end up getting stored as a floating point decimal value, no matter how they are initially defined. And, defining a hex value or the equivalent decimal value in the code will create exactly the same result in the JS interpreter.

Does it perform any implicit conversation between these two types, for example?

Other than using the right base when parsing the number, these are all just numbers in Javascript. There is no separate type for a number defined in hex versus one defined in decimal. They are just numbers. The next designation just tells the parser how to parse the initial value of the number (e.g. what base to use when parsing it).

Small demonstration:

var a = parseInt("255", 10);
var b = parseInt("FF", 16);
var c = 255;
var d = 0xFF;

log(a === b);
log(a === d);
log(b === c);
log(c === d);
<script src="https://dl.dropboxusercontent.com/u/7909102/log.js"></script>