0

im trying to get the current time timeNow in javascript and at the end of the script get timeEnd which is when the script finished executing for example:

newDate = new Date();
timeNow = newDate.getTime();

for(i=0; i < 5; i ++){

    console.log("printing" + i);
}

timeEnd = timeNow - newDate.getTime();

console.log(timeEnd);

i want to get the time it took to excute this script in seconds or milliseconds, but the result is 0. i don't see why, can you help me thanks.

7 Answers 7

5

The Date object does not keep ticking once it's constructed, it only represents a single instant in time.

You need to re-evaluate the time at the end of the script and then compare that value with the first one.

Note that on modern ES5 browsers you can use the "static" Date.now() function to get the current time. This is less expensive that constructing a whole new Date object. On older browsers you can emulate this function with this shim:

Date.now = Date.now || function() {
    return +(new Date);
};

At which point the required code becomes:

var start = Date.now();

// do your stuff here
...

var end = Date.now();

var elapsed = end - start;

FWIW, your simple loop reports 2ms when run on the Chrome console on my laptop.

Sign up to request clarification or add additional context in comments.

2 Comments

it still gives me zero, by the way im not trying to measure this simple loop, i was just giving an example.
@user1551482 I can assure you this (and the previous version) are correct.
2

This happens because you create only a single Date object. These don't change; you will have to do:

timeEnd = timeNow - new Date().getTime();

(note the blank between new and Date)

2 Comments

that's actually not legal syntax for new Date
ah, actually new Date is legal (I thought the empty constructor params were required) but the alternative would have to been to write (new Date).getTime(). I was the addition of .getTime() that broke your original answer because that . binds more tightly than new.
1
var t = new Date().getTime();
// code
console.log(new Date().getTime() - t);

Comments

0

Because it takes less than a millisecond to perform 5 iterations I would imagine.

Comments

0

you can use

starttime = Number(new Date());

endtime = Number(new Date());

Comments

0

I actually don't think it is iterating in less than an millisecond. It think the problem is that when you call newDate.getTime() repeatedly it returns the same value each time. The object, once you create it, is set at the time it was created. You'll need to create a new Date() object to compare the original against.

From my Firebug Console:

>>> d = new Date()
Date {Thu Aug 23 2012 10:45:52 GMT-0600 (Mountain Daylight Time)}
>>> d.getTime()
1345740352582
>>> d.getTime()
1345740352582
>>> d.getTime()
1345740352582
>>> d.getTime()
1345740352582

I can assure you that I didn't manually make the calls to d.getTime() in less than a millisecond.

Comments

0

Basically, Do this

newDate = new Date();
timeNow = newDate.getTime();

for(i=0; i < 50; i++){
    console.log("printing" + i);
}

futureDate = new Date()
timeEnd =  futureDate.getTime();
timeLength = timeEnd - timeNow;


console.log(timeLength);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.