Latest Moment.js vs native Date

Benchmark created on


Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Setup

Date.prototype.getStartOfDay = function() {
    this.setHours(0, 0, 0, 0)
    return this
  }
  
  Date.prototype.getStartOfWeek = function() {
    if (this.getDay()) {
      this.setHours(-Math.abs(this.getDay() - 1) * 24, 0, 0, 0)
    } else {
      this.setHours(-6 * 24, 0, 0, 0)
    }
    return this
  }
  
  Date.prototype.getStartOfMonth = function() {
    if (this.getDate()) {
      this.setHours(-Math.abs(this.getDate() - 1) * 24, 0, 0, 0)
    } else {
      this.setHours(0, 0, 0, 0)
    }
    return this
  }
  
  Date.prototype.getStartOfYear = function() {
    if (this.getDayOfYear()) {
      this.setHours(-Math.abs(this.getDayOfYear() - 1) * 24, 0, 0, 0)
    } else {
      this.setHours(0, 0, 0, 0)
    }
    return this
  }
  
  Date.prototype.getDayOfYear = function() {
    var date = this.clone(this)
    date.setMonth(0, 0)
    return Math.round((this - date) / 8.64e7)
  }
  
  Date.prototype.clone = function() {
    return new Date(this.getTime())
  }

Test runner

Ready to run.

Testing in
TestOps/sec
Moment.js Library
var momento = moment(556095600444)

var timeperiods = {
  "year": momento.clone().startOf('year').toISOString(),
  "month": momento.clone().startOf('month').toISOString(),
  "week": momento.clone().startOf('week').toISOString(),
  "day": momento.clone().startOf('day').toISOString()
}
ready
Native Date
var date = new Date(556095600444)

var timeperiods = {
  "year": date.clone().getStartOfYear().toISOString(),
  "month": date.clone().getStartOfMonth().toISOString(),
  "week": date.clone().getStartOfWeek().toISOString(),
  "day": date.clone().getStartOfDay().toISOString()
}
ready

Revisions

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