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.
This commit is contained in:
muxator 2019-02-26 23:25:15 +01:00
parent 90bfbeb38d
commit b34fc2de2b
13 changed files with 23 additions and 23 deletions

View file

@ -1,4 +1,4 @@
var startTime = new Date().getTime(); var startTime = Date.now();
var fs = require("fs"); var fs = require("fs");
var ueberDB = require("../src/node_modules/ueberDB"); var ueberDB = require("../src/node_modules/ueberDB");
var mysql = require("../src/node_modules/ueberDB/node_modules/mysql"); var mysql = require("../src/node_modules/ueberDB/node_modules/mysql");
@ -43,7 +43,7 @@ var etherpadDB = mysql.createConnection({
}); });
//get the timestamp once //get the timestamp once
var timestamp = new Date().getTime(); var timestamp = Date.now();
var padIDs; var padIDs;
@ -110,7 +110,7 @@ async.series([
function log(str) function log(str)
{ {
console.log((new Date().getTime() - startTime)/1000 + "\t" + str); console.log((Date.now() - startTime)/1000 + "\t" + str);
} }
var padsDone = 0; var padsDone = 0;
@ -121,7 +121,7 @@ function incrementPadStats()
if(padsDone%100 == 0) 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") log(padsDone + "/" + padIDs.length + "\t" + averageTime + " pad/s")
} }
} }

View file

@ -1,4 +1,4 @@
var startTime = new Date().getTime(); var startTime = Date.now();
require("ep_etherpad-lite/node_modules/npm").load({}, function(er,npm) { 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) function log(str)
{ {
console.log((new Date().getTime() - startTime)/1000 + "\t" + str); console.log((Date.now() - startTime)/1000 + "\t" + str);
} }
unescape = function(val) { unescape = function(val) {

View file

@ -522,7 +522,7 @@ exports.appendChatMessage = function(padID, text, authorID, time, callback)
if(time === undefined || !is_int(time)) if(time === undefined || !is_int(time))
{ {
// set time to current timestamp // set time to current timestamp
time = new Date().getTime(); time = Date.now();
} }
var padMessage = require("ep_etherpad-lite/node/handler/PadMessageHandler.js"); var padMessage = require("ep_etherpad-lite/node/handler/PadMessageHandler.js");

View file

@ -110,7 +110,7 @@ function mapAuthorWithDBKey (mapperkey, mapper, callback)
//there is a author with this mapper //there is a author with this mapper
//update the timestamp of this author //update the timestamp of this author
db.setSub("globalAuthor:" + author, ["timestamp"], new Date().getTime()); db.setSub("globalAuthor:" + author, ["timestamp"], Date.now());
//return the author //return the author
callback(null, {authorID: author}); callback(null, {authorID: author});
@ -127,7 +127,7 @@ exports.createAuthor = function(name, callback)
var author = "a." + randomString(16); var author = "a." + randomString(16);
//create the globalAuthors db entry //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 //set the global author db entry
db.set("globalAuthor:" + author, authorObj); db.set("globalAuthor:" + author, authorObj);

View file

@ -86,7 +86,7 @@ Pad.prototype.appendRevision = function appendRevision(aChangeset, author) {
newRevData.changeset = aChangeset; newRevData.changeset = aChangeset;
newRevData.meta = {}; newRevData.meta = {};
newRevData.meta.author = author; newRevData.meta.author = author;
newRevData.meta.timestamp = new Date().getTime(); newRevData.meta.timestamp = Date.now();
//ex. getNumForAuthor //ex. getNumForAuthor
if(author != '') if(author != '')
@ -739,7 +739,7 @@ Pad.prototype.addSavedRevision = function addSavedRevision(revNum, savedById, la
savedRevision.revNum = revNum; savedRevision.revNum = revNum;
savedRevision.savedById = savedById; savedRevision.savedById = savedById;
savedRevision.label = label || "Revision " + revNum; savedRevision.label = label || "Revision " + revNum;
savedRevision.timestamp = new Date().getTime(); savedRevision.timestamp = Date.now();
savedRevision.id = randomString(10); savedRevision.id = randomString(10);
//save this new saved revision //save this new saved revision

View file

@ -152,7 +152,7 @@ exports.checkAccess = function (padID, sessionCookie, token, password, callback)
if(ERR(err, callback)) return; 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? //is it for this group?
if(sessionInfo.groupID != groupID) if(sessionInfo.groupID != groupID)

View file

@ -114,7 +114,7 @@ exports.createSession = function(groupID, authorID, validUntil, callback)
} }
//check if validUntil is in the future //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")); callback(new customError("validUntil is in the past","apierror"));
return; return;

View file

@ -355,7 +355,7 @@ exports.handleCustomObjectMessage = function (msg, sessionID, cb) {
* @param msgString {String} the message we're sending * @param msgString {String} the message we're sending
*/ */
exports.handleCustomMessage = function (padID, msgString, cb) { exports.handleCustomMessage = function (padID, msgString, cb) {
var time = new Date().getTime(); var time = Date.now();
var msg = { var msg = {
type: 'COLLABROOM', type: 'COLLABROOM',
data: { data: {
@ -375,7 +375,7 @@ exports.handleCustomMessage = function (padID, msgString, cb) {
*/ */
function handleChatMessage(client, message) function handleChatMessage(client, message)
{ {
var time = new Date().getTime(); var time = Date.now();
var userId = sessioninfos[client.id].author; var userId = sessioninfos[client.id].author;
var text = message.data.text; var text = message.data.text;
var padId = sessioninfos[client.id].padId; var padId = sessioninfos[client.id].padId;
@ -1313,7 +1313,7 @@ function handleClientReady(client, message)
"numConnectedUsers": roomClients.length, "numConnectedUsers": roomClients.length,
"readOnlyId": padIds.readOnlyPadId, "readOnlyId": padIds.readOnlyPadId,
"readonly": padIds.readonly, "readonly": padIds.readonly,
"serverTimestamp": new Date().getTime(), "serverTimestamp": Date.now(),
"userId": author, "userId": author,
"abiwordAvailable": settings.abiwordAvailable(), "abiwordAvailable": settings.abiwordAvailable(),
"sofficeAvailable": settings.sofficeAvailable(), "sofficeAvailable": settings.sofficeAvailable(),

View file

@ -204,7 +204,7 @@ function minify(req, res, next)
res.setHeader('last-modified', date.toUTCString()); res.setHeader('last-modified', date.toUTCString());
res.setHeader('date', (new Date()).toUTCString()); res.setHeader('date', (new Date()).toUTCString());
if (settings.maxAge !== undefined) { 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('expires', expiresDate.toUTCString());
res.setHeader('cache-control', 'max-age=' + settings.maxAge); res.setHeader('cache-control', 'max-age=' + settings.maxAge);
} }

View file

@ -1071,7 +1071,7 @@ function Ace2Inner(){
function now() function now()
{ {
return (new Date()).getTime(); return Date.now();
} }
function newTimeLimit(ms) function newTimeLimit(ms)

View file

@ -475,7 +475,7 @@ var pad = {
}, },
_afterHandshake: function() _afterHandshake: function()
{ {
pad.clientTimeOffset = new Date().getTime() - clientVars.serverTimestamp; pad.clientTimeOffset = Date.now() - clientVars.serverTimestamp;
//initialize the chat //initialize the chat
chat.init(this); chat.init(this);
getParams(); getParams();

View file

@ -41,12 +41,12 @@ describe("the test helper", function(){
describe("the waitFor method", function(){ describe("the waitFor method", function(){
it("takes a timeout and waits long enough", function(done){ it("takes a timeout and waits long enough", function(done){
this.timeout(2000); this.timeout(2000);
var startTime = new Date().getTime(); var startTime = Date.now();
helper.waitFor(function(){ helper.waitFor(function(){
return false; return false;
}, 1500).fail(function(){ }, 1500).fail(function(){
var duration = new Date().getTime() - startTime; var duration = Date.now() - startTime;
expect(duration).to.be.greaterThan(1400); expect(duration).to.be.greaterThan(1400);
done(); done();
}); });

View file

@ -49,7 +49,7 @@ describe('Responsiveness of Editor', function() {
}).done(function(){ }).done(function(){
expect( inner$('div').text().length ).to.be.greaterThan( length ); // has the text changed? 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) // send some new text to the screen (ensure all 3 key events are sent)
var el = inner$('div').first(); var el = inner$('div').first();
@ -65,7 +65,7 @@ describe('Responsiveness of Editor', function() {
helper.waitFor(function(){ // Wait for the ability to process helper.waitFor(function(){ // Wait for the ability to process
return true; // Ghetto but works for now return true; // Ghetto but works for now
}).done(function(){ }).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 var delay = end - start; // get the delay as the current time minus the start time
console.log('delay:', delay); console.log('delay:', delay);