0

I have this simple function for counting Alphabets frequency.

My code is:

function getFreq(str){
  var freq={};
 str.replace(/[a-z A-Z]/g, function(match){
    freq[match] = (freq[match] || 0) + 1;
    return match;
  });
  console.log(JSON.stringify(freq));
  return freq;
}



var t0 = performance.now();
function doSomething(s){
  getFreq(s);
};
var t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " ms.")
 Enter Here : <input type="text" onchange="doSomething(this.value);" />

I have want to calculate the execution speed of getFreq function after typing some text in the input box. But here dosomething function gets run even without any data in the input box and shows same execution time even after typing some data.

2
  • 3
    why dont you put your log statement inside that method itself ? console.log("Call to doSomething took " + (t1 - t0) + " ms.") Commented Jan 25, 2017 at 7:18
  • @AmeyaDeshpande thanks you did the job for me! Thanks again Commented Jan 25, 2017 at 7:30

4 Answers 4

4

there is api console.time() and console.timeEnd(), u can use this api

for more info - https://developer.mozilla.org/en-US/docs/Web/API/Console/time

see below snippet for example.

function getFreq(str){
  var freq={};
 str.replace(/[a-z A-Z]/g, function(match){
    freq[match] = (freq[match] || 0) + 1;
    return match;
  });
  console.log(JSON.stringify(freq));
  return freq;
}



function doSomething(s){
  console.time("getFreq");
  getFreq(s);
  console.timeEnd("getFreq");
};
 Enter Here : <input type="text" onchange="doSomething(this.value);" />

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

Comments

1

console.time() starts a timer with the name concatenation, which is later stopped by console.timeEnd(). The timer names passed to both function calls have to match in order for the measuring to work.

console.time() and console.timeEnd() are only supported by modern browsers, starting with Chrome 2, Firefox 10, Safari 4, and Internet Explorer 11.

console.time("getFreq");
function getFreq(str){
   var freq={};
  str.replace(/[a-z A-Z]/g, function(match){
  freq[match] = (freq[match] || 0) + 1;
  return match;
  });

 console.log(JSON.stringify(freq));
  return freq;
}
console.timeEnd("getFreq");

Comments

1
  • It was executed before doing anything, because the performances test (t0, t1) was outside the method and it was never changed. Put the measurement inside the method.
  • I also would check the s to be empty before running the method

function getFreq(str){
  var freq={};
  str.replace(/[a-z A-Z]/g, function(match){
    freq[match] = (freq[match] || 0) + 1;
    return match;
  });
  console.log(JSON.stringify(freq));
  return freq;
}


function doSomething(s){
  if (s === "")
    return;
  var t0 = performance.now();
  getFreq(s);
  var t1 = performance.now();
  console.log("Call to doSomething took " + (t1 - t0) + " ms.")
};
Enter Here : <input type="text" onchange="doSomething(this.value);" />

1 Comment

Thanks for adding if (s === "")
1

This is what I want to do for every onChange call

function getFreq(str){
  var t0 = performance.now();
  var freq={};
 str.replace(/[a-z A-Z]/g, function(match){
    freq[match] = (freq[match] || 0) + 1;
    return match;
  });
  console.log(JSON.stringify(freq));
  var t1 = performance.now();
  console.log("Call to doSomething took " + (t1 - t0) + " ms.")
  return freq;
  
}
 <input type="text" onchange="getFreq(this.value);" />

output :

{"a":1,"b":1,"c":3," ":2,"A":1,"D":1}
Call to doSomething took 1.4000000000014552 ms.
{"a":1,"b":1,"c":4," ":3,"A":2,"D":1}
Call to doSomething took 1.9250000000029104 ms.

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.