From: Isaac Cambron Date: Mon, 21 Jul 2014 04:50:38 +0000 (-0400) Subject: optimizing clone first cut X-Git-Tag: 2.8.0~6^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cff0859eb010d0620d93b20484829b4ec758d644;p=thirdparty%2Fmoment.git optimizing clone first cut --- diff --git a/.gitignore b/.gitignore index 5a540d3cf..5794af789 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ min/tests.js sauce_connect.log .sauce-labs.creds npm-debug.log +benchmarks/results.csv diff --git a/Gruntfile.js b/Gruntfile.js index 05785cded..7b49a3827 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -186,6 +186,12 @@ module.exports = function (grunt) { moment: 'moment.js', dest: embedLocaleDest, targetLocales: embedLocaleSrc + }, + benchmark: { + all: { + src: ['benchmarks/*.js'], + dest: 'benchmarks/results.json', + } } }); diff --git a/benchmarks/clone.js b/benchmarks/clone.js new file mode 100644 index 000000000..e8a61f3d9 --- /dev/null +++ b/benchmarks/clone.js @@ -0,0 +1,10 @@ +var Benchmark = require('benchmark'), + moment = require("./../moment.js"), + base = moment('2013-05-25'); + +module.exports = { + name: 'clone', + onComplete: function(){console.log('done');}, + fn: function(){base.clone();}, + async: true +}; diff --git a/benchmarks/results.json b/benchmarks/results.json new file mode 100644 index 000000000..b3f7aaf8b --- /dev/null +++ b/benchmarks/results.json @@ -0,0 +1,23 @@ +[ + { + "name": "clone", + "timestamp": "Sun Jul 20 2014 23:14:34 GMT-0400 (EDT)", + "count": 1925, + "cycles": 3, + "hz": 34836.98543958899 + }, + { + "name": "clone", + "timestamp": "Sun Jul 20 2014 23:21:33 GMT-0400 (EDT)", + "count": 1885, + "cycles": 8, + "hz": 36659.44586569106 + }, + { + "name": "clone", + "timestamp": "Mon Jul 21 2014 00:50:03 GMT-0400 (EDT)", + "count": 11378, + "cycles": 4, + "hz": 210942.42334154248 + } +] \ No newline at end of file diff --git a/moment.js b/moment.js index 79b1e8b1c..4afc67b06 100644 --- a/moment.js +++ b/moment.js @@ -29,18 +29,18 @@ locales = {}, // moment internal properties - momentProperties = { - _isAMomentObject: null, - _i : null, - _f : null, - _l : null, - _strict : null, - _tzm : null, - _isUTC : null, - _offset : null, // optional. Combine with _isUTC - _pf : null, - _locale : null // optional - }, + momentProperties = [ + '_isAMomentObject', + '_i', + '_f', + '_l', + '_strict', + '_tzm', + '_isUTC', + '_offset', + '_pf', + '_locale' + ]; // check for nodeJS hasModule = (typeof module !== 'undefined' && module.exports), @@ -376,9 +376,11 @@ } // Moment prototype object - function Moment(config) { - checkOverflow(config); - extend(this, config); + function Moment(config, skipOverflow) { + if (skipOverflow != false) { + checkOverflow(config); + } + copyConfig(this, config); } // Duration Constructor @@ -440,15 +442,20 @@ return a; } - function cloneMoment(m) { - var result = {}, i; - for (i in m) { - if (m.hasOwnProperty(i) && momentProperties.hasOwnProperty(i)) { - result[i] = m[i]; - } + function copyConfig(to, from) { + var i, prop, val; + + for (i in momentProperties) { + prop = momentProperties[i]; + val = from[prop]; + if (typeof val !== 'undefined') { + to[prop] = val; + } } - return result; + to._d = new Date(+from._d); + + return to; } function absRound(number) { @@ -1686,9 +1693,8 @@ } if (moment.isMoment(input)) { - config = cloneMoment(input); + return new Moment(input, true); - config._d = new Date(+input._d); } else if (format) { if (isArray(format)) { makeDateFromStringAndArray(config); diff --git a/package.json b/package.json index f5958a4f7..91ab19603 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "uglify-js": "latest", "grunt": "latest", "nodeunit": "latest", + "benchmark": "latest", "grunt-contrib-jshint": "latest", "grunt-contrib-nodeunit": "latest", "grunt-contrib-concat": "latest", @@ -50,6 +51,7 @@ "grunt-env": "latest", "grunt-jscs": "latest", "grunt-karma": "latest", + "grunt-benchmark" : "latest", "load-grunt-tasks": "latest", "karma": "latest", "karma-chrome-launcher": "latest",