]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
optimizing clone first cut
authorIsaac Cambron <isaac@isaaccambron.com>
Mon, 21 Jul 2014 04:50:38 +0000 (00:50 -0400)
committerIskren Chernev <iskren.chernev@gmail.com>
Wed, 30 Jul 2014 05:30:42 +0000 (22:30 -0700)
.gitignore
Gruntfile.js
benchmarks/clone.js [new file with mode: 0644]
benchmarks/results.json [new file with mode: 0644]
moment.js
package.json

index 5a540d3cf08abfc7ce4a4dce564f956af3b2fc7e..5794af7890413fbcf90fc18031e21ecd9add54ae 100644 (file)
@@ -6,3 +6,4 @@ min/tests.js
 sauce_connect.log
 .sauce-labs.creds
 npm-debug.log
+benchmarks/results.csv
index 05785cded2f8875d8d5c83090982c317d9f2ab7e..7b49a3827396dc70c20dccd804d80fe9d4643395 100644 (file)
@@ -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 (file)
index 0000000..e8a61f3
--- /dev/null
@@ -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 (file)
index 0000000..b3f7aaf
--- /dev/null
@@ -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
index 79b1e8b1ca56fe179d99e624dabcb905138d633d..4afc67b06b038be9eab2dfad02b844978436983f 100644 (file)
--- a/moment.js
+++ b/moment.js
         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),
     }
 
     // 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
         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) {
         }
 
         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);
index f5958a4f7c30f8e79f4b84c3454b48c8a8ecaeb5..91ab19603477e66072a2def040b20af1fea4b1cf 100644 (file)
@@ -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",