Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Use Date.now() instead of +(new Date()) #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kurtextrem
Copy link

@kurtextrem kurtextrem commented Sep 20, 2018

https://jsperf.com/new-date-vs-date-now-vs-performance-now/6

Also saves a few bytes.

Another way would be to just write export default Date.now and then import {now} => import now.

@pablohpsilva
Copy link

@kurtextrem I might be wrong, but I think you have changed the now const from a function to a value. I'm not sure where this exported const will be used but I think you should just keep it as a function.

@millette
Copy link

export default Date.now

exports the function Date.now() - a value would only be right once after all.

@pablohpsilva
Copy link

You are right. My mistake.

export const now = () => {
return +new Date();
};
export const now = Date.now;
Copy link
Contributor

@fspoettel fspoettel Sep 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it would make sense to remove lib/now.mjs at this point since it just re-declares browser functionality. Instead, I'd argue for calling Date.now() directly when needed:

  • Importing now() instead of using Date.now() reduces readability of code since I have to look up how now is implemented when reading code calling the func
  • A module will lead to some overhead when used with common module bundlers (e.g. webpack), negating the bytes saved with declaring it here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree! However, I don't know if there was a plan to add other functions to now.mjs so I kept it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants