Skip to content

Commit

Permalink
Improve performance in function.js by using Array#slice directly.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobie committed Sep 30, 2008
1 parent d55932f commit 3a93b6c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,3 +1,5 @@
* Performance improvements in Function methods (Samuel Lebeau, kangax, jddalton, Tobie Langel).

*1.6.0.3* (September 29, 2008)

* Add support for the Chrome browser in jstest.rb. (Andrew Dupont)
Expand Down
24 changes: 10 additions & 14 deletions src/lang/function.js
@@ -1,10 +1,6 @@
Object.extend(Function.prototype, (function() {
var slice = Array.prototype.slice;

function toArray(args) {
return slice.call(args, 0);
}

function combine(array, args) {
var arrayLength = array.length, length = args.length;
while (length--) array[arrayLength + length] = args[length];
Expand All @@ -17,37 +13,37 @@ Object.extend(Function.prototype, (function() {
return names.length == 1 && !names[0] ? [] : names;
}

function bind() {
function bind(context) {
if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this;
var __method = this, args = toArray(arguments), object = args.shift();
var __method = this, args = slice.call(arguments, 1);
return function() {
var combinedArgs = combine(args.clone(), arguments);
return __method.apply(object, combinedArgs);
return __method.apply(context, combinedArgs);
}
}

function bindAsEventListener() {
var __method = this, args = toArray(arguments), object = args.shift();
function bindAsEventListener(context) {
var __method = this, args = slice.call(arguments, 1);
return function(event) {
var combinedArgs = combine([event || window.event], args);
return __method.apply(object, combinedArgs);
return __method.apply(context, combinedArgs);
}
}

function curry() {
if (!arguments.length) return this;
var __method = this, args = toArray(arguments);
var __method = this, args = slice.call(arguments, 0);
return function() {
var combinedArgs = combine(args.clone(), arguments);
return __method.apply(this, combinedArgs);
}
}

function delay() {
var __method = this, args = toArray(arguments), timeout = args.shift() * 1000;
function delay(timeout) {
var __method = this, args = slice.call(arguments, 1);
return window.setTimeout(function() {
return __method.apply(__method, args);
}, timeout);
}, timeout * 1000);
}

function defer() {
Expand Down

0 comments on commit 3a93b6c

Please sign in to comment.