Why is Date comparison so much slower than comparing against the result of getTime?

27 views
Skip to first unread message

Matthew Caruana Galizia

unread,
Oct 5, 2012, 12:30:31 PM10/5/12
to v8-u...@googlegroups.com
One of my colleagues wrote a test that shows that comparing two Date objects is ~95% slower than comparing against the result of getTime. Why is this so?

Florian Schneider

unread,
Oct 8, 2012, 6:10:15 AM10/8/12
to v8-u...@googlegroups.com
This is expected: When comparing two objects like Date with <, they are converted into primitive values (numbers) first. This ToPrimitive-conversion takes most of the time when comparing two Date objects. Comparing the result of getTime() on the other hand is just a comparison of two numbers which is way more efficient.

2012/10/5 Matthew Caruana Galizia <mat...@gmail.com>
One of my colleagues wrote a test that shows that comparing two Date objects is ~95% slower than comparing against the result of getTime. Why is this so?

Matthew Caruana Galizia

unread,
Oct 8, 2012, 12:15:19 PM10/8/12
to v8-u...@googlegroups.com
Thanks, that bit makes sense. Though doesn't getTime() produce a number and if so what's the difference between an explicit and implicit conversion to a primitive value?
Reply all
Reply to author
Forward
0 new messages