Negativing a positive integer

JavaScript performance comparison

Test case created by Lee

Preparation code

Benchmark.prototype.setup = function() {
    var int = 200;

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
string based
var result = Number("-"+int);
var result = Number(int * -1);
Silly but i wondered on how bad it would be...
var result = Number(int - (int * 2));

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

1 comment

Lee (revision owner) commented :

Well that was surprising (for me at least). The one i thought was silly (minusing by a double of the same number) turned out to be just as quick.

Obviously the type casting in the first one is taking it's toll.

Comment form temporarily disabled.

Add a comment