I have my dates converted to moment.js, and now I want to pare it with another date ('now' in this case).
Just a plain pare with a date object seems to be a lot faster than using moment.js isAfter
function.
- Will this simple pare work in all locales?
- Am I missing something here?
- Is there a very specific reason why isAfter seems to create a new moment object instead of taking a shortcut when it's a
Date
object?
All my dates are in UTC.
function executeTests() {
isAfterTest();
pareTest();
}
function isAfterTest() {
console.time('isAfterTest');
var now = new Date();
var dateOfBirth = moment('2000-01-01');
for (var i = 0; i < 50000; i++) {
var x = dateOfBirth.isAfter(now);
}
console.timeEnd('isAfterTest');
}
function pareTest() {
console.time('pareTest');
var now = new Date();
var dateOfBirth = moment('2000-01-01');
for (var i = 0; i < 50000; i++) {
var x = dateOfBirth > now;
}
console.timeEnd('pareTest');
}
<script src=".js/2.7.0/moment-with-langs.js"></script>
<button onclick="executeTests();">Run Test</button>
I have my dates converted to moment.js, and now I want to pare it with another date ('now' in this case).
Just a plain pare with a date object seems to be a lot faster than using moment.js isAfter
function.
- Will this simple pare work in all locales?
- Am I missing something here?
- Is there a very specific reason why isAfter seems to create a new moment object instead of taking a shortcut when it's a
Date
object?
All my dates are in UTC.
function executeTests() {
isAfterTest();
pareTest();
}
function isAfterTest() {
console.time('isAfterTest');
var now = new Date();
var dateOfBirth = moment('2000-01-01');
for (var i = 0; i < 50000; i++) {
var x = dateOfBirth.isAfter(now);
}
console.timeEnd('isAfterTest');
}
function pareTest() {
console.time('pareTest');
var now = new Date();
var dateOfBirth = moment('2000-01-01');
for (var i = 0; i < 50000; i++) {
var x = dateOfBirth > now;
}
console.timeEnd('pareTest');
}
<script src="http://cdnjs.cloudflare./ajax/libs/moment.js/2.7.0/moment-with-langs.js"></script>
<button onclick="executeTests();">Run Test</button>
Results:
isAfterTest: 3754.000ms (index):32
pareTest: 24.000ms
See: http://jsfiddle/t4grs0p7/2/
Share Improve this question edited Jan 4, 2015 at 2:00 Mr. Polywhirl 48.9k12 gold badges93 silver badges145 bronze badges asked Aug 14, 2014 at 14:10 Dirk BoerDirk Boer 9,17313 gold badges71 silver badges123 bronze badges 6-
is it even correct to pare a
Date
and aMoment
using that operator? – goat Commented Aug 14, 2014 at 14:19 - I made a jsperf for you. Feel free to tweak it if I didn't represent your test cases correctly. – ajp15243 Commented Aug 14, 2014 at 14:21
- I have not really a clue goat, but it seems to work :| that's point. I'm wondering if I'm missing some essential stuff that I'm not taking into account atm. – Dirk Boer Commented Aug 14, 2014 at 14:22
- @goat: it seems to work as far as I can see. the < operator seems to convert both objects to amount of ms since epoch through their valueOf() functions. – Dirk Boer Commented Aug 14, 2014 at 14:34
- It's quite impressive how slow the isAfter method actually is! Definitely not useful in lists of data. I just thought I'd suggest you write up your result as an answer and accept it so that it might make things easier for Google and other future browsers who e across the question. Cheers – Zac Seth Commented Nov 7, 2014 at 10:35
1 Answer
Reset to default 4Looking at the documentation http://momentjs./docs/ the isAfter method accepts different types of Date format:
moment().isAfter(Moment|String|Number|Date|Array);
This means it needs to do type checking and then convert it to a date object before running the calculation.
One way you could reduce this impact would be to pass in a Moment object as the parison date:
function isAfterTest() {
console.time('isAfterTest');
var now = moment();
var dateOfBirth = moment('2000-01-01');
for (var i = 0; i < 50000; i++) {
var x = dateOfBirth.isAfter(now);
}
console.timeEnd('isAfterTest');
}
I created a fiddle to pare, but that doesn't seem to improve it much at all: http://jsfiddle/kmturley/t4grs0p7/7/
Looking at your version I believe you should be using valueOf() method to pare the values:
window.pareTest2 = function() {
console.time('pareTest2');
var now = moment().valueOf();
var dateOfBirth = moment('2000-01-01').valueOf();
for ( var i = 0; i < 50000; i++ )
var x = dateOfBirth > now;
console.timeEnd('pareTest2');
}
Here is a working example: http://jsfiddle/kmturley/t4grs0p7/8/