From b34fc2de2b40138275275596a790358fe1e130d9 Mon Sep 17 00:00:00 2001 From: muxator Date: Tue, 26 Feb 2019 23:25:15 +0100 Subject: [PATCH] use Date.now() instead of new Date().getTime() This is documented to be more performant. The substitution was made on frontend code, too (i.e., the one in /static), because Date.now() is supported since IE 9, and we are life supporting only IE 11. Commands: find . -name *.js | xargs sed --in-place "s/new Date().getTime()/Date.now()/g" find . -name *.js | xargs sed --in-place "s/(new Date()).getTime()/Date.now()/g" Not done on jQuery. --- bin/convert.js | 8 ++++---- bin/importSqlFile.js | 4 ++-- src/node/db/API.js | 2 +- src/node/db/AuthorManager.js | 4 ++-- src/node/db/Pad.js | 4 ++-- src/node/db/SecurityManager.js | 2 +- src/node/db/SessionManager.js | 2 +- src/node/handler/PadMessageHandler.js | 6 +++--- src/node/utils/Minify.js | 2 +- src/static/js/ace2_inner.js | 2 +- src/static/js/pad.js | 2 +- tests/frontend/specs/helper.js | 4 ++-- tests/frontend/specs/responsiveness.js | 4 ++-- 13 files changed, 23 insertions(+), 23 deletions(-) diff --git a/bin/convert.js b/bin/convert.js index 757602c29..5b04b2db0 100644 --- a/bin/convert.js +++ b/bin/convert.js @@ -1,4 +1,4 @@ -var startTime = new Date().getTime(); +var startTime = Date.now(); var fs = require("fs"); var ueberDB = require("../src/node_modules/ueberDB"); var mysql = require("../src/node_modules/ueberDB/node_modules/mysql"); @@ -43,7 +43,7 @@ var etherpadDB = mysql.createConnection({ }); //get the timestamp once -var timestamp = new Date().getTime(); +var timestamp = Date.now(); var padIDs; @@ -110,7 +110,7 @@ async.series([ function log(str) { - console.log((new Date().getTime() - startTime)/1000 + "\t" + str); + console.log((Date.now() - startTime)/1000 + "\t" + str); } var padsDone = 0; @@ -121,7 +121,7 @@ function incrementPadStats() if(padsDone%100 == 0) { - var averageTime = Math.round(padsDone/((new Date().getTime() - startTime)/1000)); + var averageTime = Math.round(padsDone/((Date.now() - startTime)/1000)); log(padsDone + "/" + padIDs.length + "\t" + averageTime + " pad/s") } } diff --git a/bin/importSqlFile.js b/bin/importSqlFile.js index 5cdf46e5d..25592438f 100644 --- a/bin/importSqlFile.js +++ b/bin/importSqlFile.js @@ -1,4 +1,4 @@ -var startTime = new Date().getTime(); +var startTime = Date.now(); require("ep_etherpad-lite/node_modules/npm").load({}, function(er,npm) { @@ -73,7 +73,7 @@ require("ep_etherpad-lite/node_modules/npm").load({}, function(er,npm) { function log(str) { - console.log((new Date().getTime() - startTime)/1000 + "\t" + str); + console.log((Date.now() - startTime)/1000 + "\t" + str); } unescape = function(val) { diff --git a/src/node/db/API.js b/src/node/db/API.js index 6d6c8ccff..a95723451 100644 --- a/src/node/db/API.js +++ b/src/node/db/API.js @@ -522,7 +522,7 @@ exports.appendChatMessage = function(padID, text, authorID, time, callback) if(time === undefined || !is_int(time)) { // set time to current timestamp - time = new Date().getTime(); + time = Date.now(); } var padMessage = require("ep_etherpad-lite/node/handler/PadMessageHandler.js"); diff --git a/src/node/db/AuthorManager.js b/src/node/db/AuthorManager.js index c7ebf47f4..bcb6d393d 100644 --- a/src/node/db/AuthorManager.js +++ b/src/node/db/AuthorManager.js @@ -110,7 +110,7 @@ function mapAuthorWithDBKey (mapperkey, mapper, callback) //there is a author with this mapper //update the timestamp of this author - db.setSub("globalAuthor:" + author, ["timestamp"], new Date().getTime()); + db.setSub("globalAuthor:" + author, ["timestamp"], Date.now()); //return the author callback(null, {authorID: author}); @@ -127,7 +127,7 @@ exports.createAuthor = function(name, callback) var author = "a." + randomString(16); //create the globalAuthors db entry - var authorObj = {"colorId" : Math.floor(Math.random()*(exports.getColorPalette().length)), "name": name, "timestamp": new Date().getTime()}; + var authorObj = {"colorId" : Math.floor(Math.random()*(exports.getColorPalette().length)), "name": name, "timestamp": Date.now()}; //set the global author db entry db.set("globalAuthor:" + author, authorObj); diff --git a/src/node/db/Pad.js b/src/node/db/Pad.js index 4e7d16987..b74de5228 100644 --- a/src/node/db/Pad.js +++ b/src/node/db/Pad.js @@ -86,7 +86,7 @@ Pad.prototype.appendRevision = function appendRevision(aChangeset, author) { newRevData.changeset = aChangeset; newRevData.meta = {}; newRevData.meta.author = author; - newRevData.meta.timestamp = new Date().getTime(); + newRevData.meta.timestamp = Date.now(); //ex. getNumForAuthor if(author != '') @@ -739,7 +739,7 @@ Pad.prototype.addSavedRevision = function addSavedRevision(revNum, savedById, la savedRevision.revNum = revNum; savedRevision.savedById = savedById; savedRevision.label = label || "Revision " + revNum; - savedRevision.timestamp = new Date().getTime(); + savedRevision.timestamp = Date.now(); savedRevision.id = randomString(10); //save this new saved revision diff --git a/src/node/db/SecurityManager.js b/src/node/db/SecurityManager.js index f930b9618..2c46ac508 100644 --- a/src/node/db/SecurityManager.js +++ b/src/node/db/SecurityManager.js @@ -152,7 +152,7 @@ exports.checkAccess = function (padID, sessionCookie, token, password, callback) if(ERR(err, callback)) return; - var now = Math.floor(new Date().getTime()/1000); + var now = Math.floor(Date.now()/1000); //is it for this group? if(sessionInfo.groupID != groupID) diff --git a/src/node/db/SessionManager.js b/src/node/db/SessionManager.js index 954203758..99803ee71 100644 --- a/src/node/db/SessionManager.js +++ b/src/node/db/SessionManager.js @@ -114,7 +114,7 @@ exports.createSession = function(groupID, authorID, validUntil, callback) } //check if validUntil is in the future - if(Math.floor(new Date().getTime()/1000) > validUntil) + if(Math.floor(Date.now()/1000) > validUntil) { callback(new customError("validUntil is in the past","apierror")); return; diff --git a/src/node/handler/PadMessageHandler.js b/src/node/handler/PadMessageHandler.js index 5581dbeb9..506b3aae4 100644 --- a/src/node/handler/PadMessageHandler.js +++ b/src/node/handler/PadMessageHandler.js @@ -355,7 +355,7 @@ exports.handleCustomObjectMessage = function (msg, sessionID, cb) { * @param msgString {String} the message we're sending */ exports.handleCustomMessage = function (padID, msgString, cb) { - var time = new Date().getTime(); + var time = Date.now(); var msg = { type: 'COLLABROOM', data: { @@ -375,7 +375,7 @@ exports.handleCustomMessage = function (padID, msgString, cb) { */ function handleChatMessage(client, message) { - var time = new Date().getTime(); + var time = Date.now(); var userId = sessioninfos[client.id].author; var text = message.data.text; var padId = sessioninfos[client.id].padId; @@ -1313,7 +1313,7 @@ function handleClientReady(client, message) "numConnectedUsers": roomClients.length, "readOnlyId": padIds.readOnlyPadId, "readonly": padIds.readonly, - "serverTimestamp": new Date().getTime(), + "serverTimestamp": Date.now(), "userId": author, "abiwordAvailable": settings.abiwordAvailable(), "sofficeAvailable": settings.sofficeAvailable(), diff --git a/src/node/utils/Minify.js b/src/node/utils/Minify.js index 4596f404c..6f3b75186 100644 --- a/src/node/utils/Minify.js +++ b/src/node/utils/Minify.js @@ -204,7 +204,7 @@ function minify(req, res, next) res.setHeader('last-modified', date.toUTCString()); res.setHeader('date', (new Date()).toUTCString()); if (settings.maxAge !== undefined) { - var expiresDate = new Date((new Date()).getTime()+settings.maxAge*1000); + var expiresDate = new Date(Date.now()+settings.maxAge*1000); res.setHeader('expires', expiresDate.toUTCString()); res.setHeader('cache-control', 'max-age=' + settings.maxAge); } diff --git a/src/static/js/ace2_inner.js b/src/static/js/ace2_inner.js index a34b94e59..597e8451a 100644 --- a/src/static/js/ace2_inner.js +++ b/src/static/js/ace2_inner.js @@ -1071,7 +1071,7 @@ function Ace2Inner(){ function now() { - return (new Date()).getTime(); + return Date.now(); } function newTimeLimit(ms) diff --git a/src/static/js/pad.js b/src/static/js/pad.js index a72225979..b12058237 100644 --- a/src/static/js/pad.js +++ b/src/static/js/pad.js @@ -475,7 +475,7 @@ var pad = { }, _afterHandshake: function() { - pad.clientTimeOffset = new Date().getTime() - clientVars.serverTimestamp; + pad.clientTimeOffset = Date.now() - clientVars.serverTimestamp; //initialize the chat chat.init(this); getParams(); diff --git a/tests/frontend/specs/helper.js b/tests/frontend/specs/helper.js index 8520769aa..727e47960 100644 --- a/tests/frontend/specs/helper.js +++ b/tests/frontend/specs/helper.js @@ -41,12 +41,12 @@ describe("the test helper", function(){ describe("the waitFor method", function(){ it("takes a timeout and waits long enough", function(done){ this.timeout(2000); - var startTime = new Date().getTime(); + var startTime = Date.now(); helper.waitFor(function(){ return false; }, 1500).fail(function(){ - var duration = new Date().getTime() - startTime; + var duration = Date.now() - startTime; expect(duration).to.be.greaterThan(1400); done(); }); diff --git a/tests/frontend/specs/responsiveness.js b/tests/frontend/specs/responsiveness.js index ff7dace17..dec7b8dce 100644 --- a/tests/frontend/specs/responsiveness.js +++ b/tests/frontend/specs/responsiveness.js @@ -49,7 +49,7 @@ describe('Responsiveness of Editor', function() { }).done(function(){ expect( inner$('div').text().length ).to.be.greaterThan( length ); // has the text changed? - var start = new Date().getTime(); // get the start time + var start = Date.now(); // get the start time // send some new text to the screen (ensure all 3 key events are sent) var el = inner$('div').first(); @@ -65,7 +65,7 @@ describe('Responsiveness of Editor', function() { helper.waitFor(function(){ // Wait for the ability to process return true; // Ghetto but works for now }).done(function(){ - var end = new Date().getTime(); // get the current time + var end = Date.now(); // get the current time var delay = end - start; // get the delay as the current time minus the start time console.log('delay:', delay);