id++ vs nextUniqueId

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var MIN_PRECISION = -Math.pow(2, 53),
                MAX_PRECISION = -MIN_PRECISION,
                // idNum will ensure identifiers are unique.
                idNum = [ MIN_PRECISION ];
   
    var id = MIN_PRECISION;
    var x;
    var join = Function.prototype.call.bind(Array.prototype.join);
   
    function nextUniqueId() {
                idNum[0]++;
                for(var i = 0; idNum[i] >= MAX_PRECISION && i < idNum.length; i++) {
                        idNum[i] = MIN_PRECISION;
                        if (i < idNum.length) idNum[i + 1]++;
                        else {
                                // Reset and add a digit.
                                idNum = map(idNum, function() { return MIN_PRECISION; });
                                push(idNum, MIN_PRECISION);
                                break;
                        }
                }
                return '{' + join(idNum, ',') + '}';
        }
};

Benchmark.prototype.teardown = function() {
    id = null; idNum = null;
    x = null;
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
id++
x = id++;
pending…
nextUniqueId
x = nextUniqueId();
pending…

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

Compare results of other browsers

0 comments

Add a comment