d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Js Task: Write A Timing Tracker Function > 100fg
Add Reply New Topic New Poll
Member
Posts: 14,925
Joined: Jan 3 2008
Gold: 135,879.75
Dec 5 2019 12:37am
Code
function timingTracker(fn) {
}


* The timingTracker will accept a function as its argument.
* It will allow the function to run as normal.
* After, it will log how long it took for the function to elapse.
* Criteria: Do not use console.time or console.timeEnd.

This post was edited by kdog3682 on Dec 5 2019 12:38am
Member
Posts: 17,090
Joined: Nov 22 2008
Gold: 169.00
Dec 5 2019 09:51am
Quote (kdog3682 @ Dec 5 2019 07:37am)
Code
function timingTracker(fn) {
}


* The timingTracker will accept a function as its argument.
* It will allow the function to run as normal.
* After, it will log how long it took for the function to elapse.
* Criteria: Do not use console.time or console.timeEnd.


Solution #1: Using performance.now():
Code
function doSomething(){
for (i = 0; i < 500000; i++) {
}
}

function timingTracker(passedFunction){
var start = performance.now();

passedFunction();

var end = performance.now();
console.log("Passed function took " + (end - start) + " milliseconds to execute.");
}

timingTracker(doSomething);


#Solution #2: Using new Date()
Code
function doSomething() {
for (i = 0; i < 500000; i++) {
}
}

function timingTracker(passedFunction) {
var start = new Date()

passedFunction();

var end = new Date()
var timeDifference = end - start; //in ms
timeDifference /= 1000; //strip the ms

console.log("Passed function took " + timeDifference + " seconds to execute.");
}

timingTracker(doSomething);



There is a bunch of other ways to do it without console.time or console.timeEnd but these two are the most popular.
Just dont not use stuff like setInterval() as they will provide very inconsistent results due to event loop and also modern browsers often deprioritise these calls for inactive browser tabs to save on CPU usage.

Let me know if something was unclear.

This post was edited by WeAreTheBorg on Dec 5 2019 09:53am
Member
Posts: 14,925
Joined: Jan 3 2008
Gold: 135,879.75
Dec 5 2019 09:14pm
Quote (WeAreTheBorg @ Dec 5 2019 10:51am)
Solution #1: Using performance.now():
Code
function doSomething(){
for (i = 0; i < 500000; i++) {
}
}

function timingTracker(passedFunction){
var start = performance.now();

passedFunction();

var end = performance.now();
console.log("Passed function took " + (end - start) + " milliseconds to execute.");
}

timingTracker(doSomething);


#Solution #2: Using new Date()
Code
function doSomething() {
for (i = 0; i < 500000; i++) {
}
}

function timingTracker(passedFunction) {
var start = new Date()

passedFunction();

var end = new Date()
var timeDifference = end - start; //in ms
timeDifference /= 1000; //strip the ms

console.log("Passed function took " + timeDifference + " seconds to execute.");
}

timingTracker(doSomething);



There is a bunch of other ways to do it without console.time or console.timeEnd but these two are the most popular.
Just dont not use stuff like setInterval() as they will provide very inconsistent results due to event loop and also modern browsers often deprioritise these calls for inactive browser tabs to save on CPU usage.

Let me know if something was unclear.


The code doesn't seem to work for:

Code
function doSomething(a, b) {
return a + b
}


Also, should new Date() be Date.now() ?

This post was edited by kdog3682 on Dec 5 2019 09:23pm
Member
Posts: 17,090
Joined: Nov 22 2008
Gold: 169.00
Dec 6 2019 03:45am
Quote (kdog3682 @ Dec 6 2019 04:14am)
The code doesn't seem to work for:

Code
function doSomething(a, b) {
return a + b
}


Also, should new Date() be Date.now() ?


Hi, yes, you can use Date.now() if you are going to count ms only.
If you want to manipulate dates, like printing exact date and time of execution, new Date() is a way to go since it has bunch of in-built functions added.

Now, about passing an arguments to a function that has been passed to another function, you can mock it on several ways.
But there is a cool thing Function.prototype.bind() and you can go with it. You can read more about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind

If you dont want to read documentation, and want to go straight to example, here is bind() usage in one simple example:

Code
function doSomething(number1, number2){
for (i = 0; i < number1; i++) {
}

for (i = 0; i < number2; i++) {
}
}

function timingTracker(passedFunction){
var start = performance.now();

passedFunction();

var end = performance.now();
console.log("Passed function took " + (end - start) + " milliseconds to execute.");
}

timingTracker(doSomething.bind(null, 500000, 200000));

Member
Posts: 2,619
Joined: May 21 2004
Gold: 21,934.00
Dec 6 2019 12:20pm
Just to add a bit about how you would do this in the wild without using bind:

Code
function timingTracker(fn, args) {
const start = performance.now();
fn.apply(this, args);
const end = performance.now();
// output start and end delta however you like.
}


Of course the original example was just taking a normal function signature, but you dont usually see non aop solution timing stuff like this without args as the second argument for use with apply(), because as you can see above bind() is kind of ugly.

Additionally, use performance.now() instead of Date. It's better supported across browsers.

This post was edited by frixionburne on Dec 6 2019 12:23pm
Member
Posts: 14,925
Joined: Jan 3 2008
Gold: 135,879.75
Dec 8 2019 10:13am
Quote (frixionburne @ Dec 6 2019 01:20pm)
Just to add a bit about how you would do this in the wild without using bind:

Code
function timingTracker(fn, args) {
const start = performance.now();
fn.apply(this, args);
const end = performance.now();
// output start and end delta however you like.
}


Of course the original example was just taking a normal function signature, but you dont usually see non aop solution timing stuff like this without args as the second argument for use with apply(), because as you can see above bind() is kind of ugly.

Additionally, use performance.now() instead of Date. It's better supported across browsers.


Thanks.

Quote (WeAreTheBorg @ Dec 6 2019 04:45am)
Hi, yes, you can use Date.now() if you are going to count ms only.
If you want to manipulate dates, like printing exact date and time of execution, new Date() is a way to go since it has bunch of in-built functions added.

Now, about passing an arguments to a function that has been passed to another function, you can mock it on several ways.
But there is a cool thing Function.prototype.bind() and you can go with it. You can read more about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind

If you dont want to read documentation, and want to go straight to example, here is bind() usage in one simple example:
Code
function doSomething(number1, number2){
for (i = 0; i < number1; i++) {
}

for (i = 0; i < number2; i++) {
}
}

function timingTracker(passedFunction){
var start = performance.now();

passedFunction();

var end = performance.now();
console.log("Passed function took " + (end - start) + " milliseconds to execute.");
}

timingTracker(doSomething.bind(null, 500000, 200000));


Thanks.

This post was edited by kdog3682 on Dec 8 2019 10:14am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll