paralleldrive / cuid

Collision-resistant ids optimized for horizontal scaling and performance.
Other
3.42k stars 123 forks source link

Usage of new Date().getTime() vs Date.now #267

Closed Compositr closed 1 year ago

Compositr commented 1 year ago

On line 42 the timestamp generated by creating a new Date instance and calling the getTime method on it.

However, as shown by this benchmark, Date.now is ~15% faster than creating a new Date instance. Is there any specific reason to not use Date.now?

ericelliott commented 1 year ago

Date.now() was added to the ECMAScript spec in ES5. The original CUID implementation predates ES5's widespread availability by 2-3 years.

The slow path takes 100 - 120ms to calculate 1 million dates. The fast path takes 90 - 110ms. This is a tiny micro-optimization. You'd need to be generating hundreds of thousands of CUIDs in a tight loop (like a render loop) for this to make any notable impact with users. Is this actually a bottleneck for you, and if so, in what situation?

I'm going to close this issue for now. Feel free to re-open if this is actually causing a problem for you.