forked from bazaarvoice/bv-ui-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnow.js
More file actions
63 lines (55 loc) · 1.86 KB
/
Copy pathnow.js
File metadata and controls
63 lines (55 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* @fileOverview Provides a cross-browser safe way to use the Navigation Timing API
* Ported from Firebird's scout directory
* Based on https://gist.github.com/paulirish/5438650
*
* Note: this is not a true polyfill in browsers that do not implement the
* Navigation Timing API. Rather than returning the time elapsed since
* `navigationStart`, it will return the time elapsed since the polyfill was
* installed.
*
* Further reading:
* http://www.w3.org/TR/user-timing/
* http://caniuse.com/#search=User%20Timing
* http://caniuse.com/#search=performance.now
* http://caniuse.com/#search=Navigation%20Timing
*/
// Imports
var global = require('../global');
var dateNow = require('../date.now');
// Cache references for speed and to guard against shenanigans
var performance = global.performance;
var nativeNow = performance && performance.now;
// Is the Navigation Timing API natively supported?
var isNativeSupported = (typeof nativeNow === 'function');
// Used by the polyfill implementation to get the moment the module was installed
var nowOffset = dateNow.now();
// Use navigationStart as the offset when possible
if (performance &&
performance.timing &&
typeof performance.timing.navigationStart === 'number'
) {
// Use navigationStart value instead
nowOffset = performance.timing.navigationStart;
}
/**
* Native Implementation
* Uses the natively-supported performance.now function
*/
function nativeImplementation () {
return nativeNow.call(performance);
}
/**
* Polyfill Implementation
* Provides similar functionality to performance.now but instead uses a default
* offset of the moment the polyfill was installed
*/
function polyfillImplementation () {
return dateNow.now() - nowOffset;
}
module.exports = {
now: function () {
var func = (isNativeSupported) ? nativeImplementation : polyfillImplementation;
return func();
}
};