+new Date vs Date.now() vs performance.now()

JavaScript performance comparison

Revision 17 of this test case created

Info

https://twitter.com/jaffathecake/status/306706682552143872

jaffathecake: Avoid "+new Date" to generate timestamps during animation. Use "Date.now()" or "performance.now()" instead http://t.co/Hle0qzxWeZ

Preparation code

<script>
window.saveIt = function(x){
    var saveIt2 = function(y){Math.pow(y, 1.1)}
    var saveIt3 = function(y){Math.pow(y, 1.1)}
    if (x % 2) {
        saveIt2(x);
    }
    else {
        saveIt3(x);
    }

   try {throw 1}
   catch(e){}
};
</script>
<script>
Benchmark.prototype.setup = function() {
    var perf = performance;
    var result = 0;
};

Benchmark.prototype.teardown = function() {
    // use the result to prevent the code being thrown out
    window.saveIt(result);
};
</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
Date.now
result = Date.now();
pending…
+new Date()
result = +new Date();
pending…
performance.now()
result = performance.now()
pending…
new Date().getTime()
result = new Date().getTime()
pending…
new Date().valueOf()
result = new Date().valueOf()
pending…
Cached performance object
result = perf.now()
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment