What if there is overhead due to the transfer of primitive JavaScript values to become objects?
For instance:
> var x = Object(12); undefined > typeof x "object" > ({}).toString.call(x); "[object Number]"
vs
> var y = 12; undefined > typeof y "number" > ({}).toString.call(y); "[object Number]"
My reason for asking: I am working with GWT code that converts values between JavaScript and Java code (which is later compiled into Javascript code). Since Java cares a lot about types, and the GWT JSNI (interop method) supports primitives and objects, I was wondering why not just returning boxed primitives (for example, Object (12) is a boxed version of 12).
The answer is that I expected a clear speed. I didn’t understand that it would be so hard, but I explicitly avoid using Javascript primitives if you can or pay a serious price for performance. (with the caveat that the overhead from calling the method is flipped around, as Yang says)
source share