Skip to content

Commit

Permalink
merging in iamnoah's optimizations for the isType family of functions…
Browse files Browse the repository at this point in the history
…, and other references to core prototoypes
  • Loading branch information
jashkenas committed Dec 9, 2009
1 parent e863fbf commit 225d795
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 35 deletions.
25 changes: 0 additions & 25 deletions test/speed.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,6 @@
var objects = _.map(numbers, function(n){ return {num : n}; });
var randomized = _.sortBy(numbers, function(){ return Math.random(); });

JSLitmus.test('_.isNumber()', function() {
_.isNumber(123);
_.isNumber(null);
_.isNumber(NaN);
_.isNumber(/foo/);
_.isNumber("abc");
});

JSLitmus.test('_.isString()', function() {
_.isString(123);
_.isString(null);
_.isString(NaN);
_.isString(/foo/);
_.isString("abc");
});

JSLitmus.test('_.isDate()', function() {
_.isDate(123);
_.isDate(null);
_.isDate(NaN);
_.isDate(/foo/);
_.isDate("abc");
_.isDate(new Date());
});

JSLitmus.test('_.each()', function() {
var timesTwo = [];
_.each(numbers, function(num){ timesTwo.push(num * 2); });
Expand Down
22 changes: 12 additions & 10 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
// Export the Underscore object for CommonJS.
if (typeof exports !== 'undefined') exports._ = _;

// Create quick reference variables for speed access to Object.prototype.
var toString = Object.prototype.toString, hasOwnProperty = Object.prototype.hasOwnProperty;

// Current version.
_.VERSION = '0.5.0';

Expand Down Expand Up @@ -391,7 +394,6 @@
/* ------------------------- Object Functions: ---------------------------- */

// Retrieve the names of an object's properties.
var hasOwnProperty = Object.prototype.hasOwnProperty;
_.keys = function(obj) {
if(_.isArray(obj)) return _.range(0, obj.length);
var keys = [];
Expand Down Expand Up @@ -477,18 +479,19 @@
_.isUndefined = function(obj) {
return typeof obj == 'undefined';
};

// have to define isNumber before _.each will work in IE

// isNumber needs to be defined before the rest of the isType functions,
// because _.each uses it in IE (and we want to use _.each for the closure).
_.isNumber = function(obj) {
return Object.prototype.toString.call(obj) == '[object Number]';
return toString.call(obj) == '[object Number]';
};

// Define the isArray, isDate, isFunction, isRegExp, and
// isString functions based on their toString identifiers.
// Define the isArray, isDate, isFunction, isRegExp, and isString functions
// based on their toString identifiers.
_.each(['Array', 'Date', 'Function', 'RegExp', 'String'], function(type) {
var toString = Object.prototype.toString, typeString = '[object ' + type + ']';
var identifier = '[object ' + type + ']';
_['is' + type] = function(obj) {
return toString.call(obj) == typeString;
return toString.call(obj) == identifier;
};
});

Expand Down Expand Up @@ -563,8 +566,7 @@

// Add all of the Underscore functions to the wrapper object.
_.each(_.functions(_), function(name) {
var unshift = Array.prototype.unshift,
method = _[name];
var method = _[name], unshift = Array.prototype.unshift;
wrapper.prototype[name] = function() {
unshift.call(arguments, this._wrapped);
return result(method.apply(_, arguments), this._chain);
Expand Down

0 comments on commit 225d795

Please sign in to comment.