]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Add some optimizations to often used functions 1893/head
authorMathias Küsel <mathiask@hotmail.de>
Tue, 12 Jan 2016 08:47:55 +0000 (09:47 +0100)
committerMathias Küsel <mathiask@hotmail.de>
Tue, 12 Jan 2016 08:47:55 +0000 (09:47 +0100)
src/core/core.helpers.js

index 8607dd00d14436b19a5d82afe1a056d675961162..07a4488c775ad8fc422c12ec740dbf685ec6abb3 100644 (file)
 
        //-- Basic js utility methods
        helpers.each = function(loopable, callback, self, reverse) {
-               var additionalArgs = Array.prototype.slice.call(arguments, 3);
                // Check to see if null or undefined firstly.
-               if (loopable) {
-                       if (loopable.length === +loopable.length) {
-                               var i;
-                               if (reverse) {
-                                       for (i = loopable.length - 1; i >= 0; i--) {
-                                               callback.apply(self, [loopable[i], i].concat(additionalArgs));
-                                       }
-                               } else {
-                                       for (i = 0; i < loopable.length; i++) {
-                                               callback.apply(self, [loopable[i], i].concat(additionalArgs));
-                                       }
+               var i, len;
+               if (helpers.isArray(loopable)) {
+                       len = loopable.length;
+                       if (reverse) {
+                               for (i = len - 1; i >= 0; i--) {
+                                       callback.call(self, loopable[i], i);
                                }
                        } else {
-                               for (var item in loopable) {
-                                       callback.apply(self, [loopable[item], item].concat(additionalArgs));
+                               for (i = 0; i < len; i++) {
+                                       callback.call(self, loopable[i], i);
                                }
                        }
+               } else if (typeof loopable === 'object') {
+                       var keys = Object.keys(loopable);
+                       len = keys.length;
+                       for (i = 0; i < len; i++) {
+                               callback.call(self, loopable[keys[i]], keys[i]);
+                       }
                }
        };
        helpers.clone = function(obj) {
                return objClone;
        };
        helpers.extend = function(base) {
-               helpers.each(Array.prototype.slice.call(arguments, 1), function(extensionObject) {
+               var len = arguments.length;
+               var additionalArgs = [];
+               for(var i = 1; i < len; i++) {
+                       additionalArgs.push(arguments[i]);
+               }
+               helpers.each(additionalArgs, function(extensionObject) {
                        helpers.each(extensionObject, function(value, key) {
                                if (extensionObject.hasOwnProperty(key)) {
                                        base[key] = value;