Skip to content

Commit 072eaf0

Browse files
committed
Expose _.now
Expose the internal function getTime as _.now and alias _.getTime
1 parent e6b0ae3 commit 072eaf0

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

test/utility.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ $(document).ready(function() {
4949
}), "should produce a random number when passed `Number.MAX_VALUE`");
5050
});
5151

52+
test("now", function() {
53+
var diff = _.now() - new Date().getTime();
54+
ok(diff <= 0 && diff > -5, "Produces the correct time in milliseconds");//within 5ms
55+
});
56+
5257
test("uniqueId", function() {
5358
var ids = [], i = 0;
5459
while(i++ < 100) ids.push(_.uniqueId());

underscore.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@
2020
// Save bytes in the minified (but not gzipped) version:
2121
var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
2222

23-
//use the faster Date.now if available.
24-
var getTime = (Date.now || function() {
25-
return new Date().getTime();
26-
});
27-
2823
// Create quick reference variables for speed access to core prototypes.
2924
var
3025
push = ArrayProto.push,
@@ -679,13 +674,13 @@
679674
var previous = 0;
680675
options || (options = {});
681676
var later = function() {
682-
previous = options.leading === false ? 0 : getTime();
677+
previous = options.leading === false ? 0 : _.now();
683678
timeout = null;
684679
result = func.apply(context, args);
685680
context = args = null;
686681
};
687682
return function() {
688-
var now = getTime();
683+
var now = _.now();
689684
if (!previous && options.leading === false) previous = now;
690685
var remaining = wait - (now - previous);
691686
context = this;
@@ -712,9 +707,9 @@
712707
return function() {
713708
context = this;
714709
args = arguments;
715-
timestamp = getTime();
710+
timestamp = _.now();
716711
var later = function() {
717-
var last = getTime() - timestamp;
712+
var last = _.now() - timestamp;
718713
if (last < wait) {
719714
timeout = setTimeout(later, wait - last);
720715
} else {
@@ -1111,6 +1106,11 @@
11111106
return min + Math.floor(Math.random() * (max - min + 1));
11121107
};
11131108

1109+
//use the faster Date.now if available.
1110+
_.now = _.getTime = (Date.now || function() {
1111+
return new Date().getTime();
1112+
});
1113+
11141114
// List of HTML entities for escaping.
11151115
var entityMap = {
11161116
escape: {

0 commit comments

Comments
 (0)