timestamping

JavaScript performance comparison

Test case created by Mark Macdonald

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function toTimestamp(date) {
      return date.getUTCFullYear()
          + ("0" + (1 + date.getUTCMonth())).slice(-2)
          + ("0" + date.getUTCDate()).slice(-2)
          + ("0" + date.getUTCHours()).slice(-2)
          + ("0" + date.getUTCMinutes()).slice(-2)
          + ("0" + date.getUTCSeconds()).slice(-2);
  }
  
  function toTimestampReduce(date) {
      return date.getUTCFullYear() + ["Month","Date","Hours","Minutes","Seconds"].reduce((str, method) => {
          const value = date["getUTC" + method]() + (method === "Month" ? 1 : 0)
          return str + ("0" + value).slice(-2)
      }, "");
  }
  
  
  function toTimestampSliceAfter(date) {
      return date.getUTCFullYear()
          + [
              date.getUTCMonth() + 1,
              date.getUTCDate(),
              date.getUTCHours(),
              date.getUTCMinutes(),
              date.getUTCSeconds()
          ].map(i => ("0" + i).slice(-2)).join("")
  }
  

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
reduce
var timestamp = toTimestampReduce(new Date())
pending…
slice later
var timestamp = toTimestampSliceAfter(new Date())
pending…
plain
var timestamp = toTimestamp(new Date())
pending…

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

0 Comments