MomentJs Expensive ops

JavaScript performance comparison

Test case created by Joshua

Info

I'm having some performance issues with a calendar app using moment js and I want to see which operations are more expensive.

Preparation code

<script src="//momentjs.com/downloads/moment-with-langs.min.js"></script>
 
<script>
Benchmark.prototype.setup = function() {
    m = moment.parseZone("2014-07-05T09:00:00Z");
    m50 = m.clone().add('year', 50);
    dayOfWeek = 3;
    xOfMonth = 2;
   
   
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Clone
x = m.clone()
pending…
Add year
x = m.clone().add('year', 1)
pending…
Get Month of Year
// Returns a particular month for this year
x = m.clone().startOf('year').add('month', 3)
pending…
Get Day of Month
x = m.clone().set('date', (7 * xOfMonth)  + (7 - m.clone().startOf('month').get('day') + dayOfWeek) % 7 - 6)
pending…
StartOf
m.clone().startOf('year')
pending…
Diff
m.diff(m50, 'year')
pending…
Add many Years
m.clone().add('year', 50)
pending…
Diff floor
Math.floor(m.diff(m50, 'year', true));
pending…
Full setPos
m.clone().set('month', 2).startOf('year').add('month', 3)
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

1 comment

Joshua (revision owner) commented :

As you can see the diff operation is an order of magnitude slower than the other moment.js operations.

Add a comment