zilverline / react-tap-event-plugin

Instant TapEvents for React
http://facebook.github.io/react/
MIT License
1.07k stars 110 forks source link

Generate now function ahead of time #21

Closed tomhicks-bsf closed 9 years ago

tomhicks-bsf commented 9 years ago

I'm not sure if this code is hit often enough to make this micro-optimisation worth it, but we can generate the now function at load-time rather than checking how to get it every time we call it.

s0meone commented 9 years ago

Doesn't this lock the timestamp to a single value? Or am I missing something? now doesn't return a function, but an actual value.

> var now = (function() { return +new Date; })()
undefined
> now
1432197198304
> now
1432197198304
> now
1432197198304
tomhicks-bsf commented 9 years ago

It returns a function that will call +new Date. You're just returning the value, not a function:

var now = (function () {
  return function () { return +new Date; };
})();

as opposed to

var now = (function () {
  return return +new Date;
})();

So the now function will be one of two functions: the IE one or the proper one.

s0meone commented 9 years ago

Ah, yes, of course. Didn't look close enough. Looks good :+1: Thanks!