Use Js to compute function average running time

Two years later and your browser most likely supports performance.now(). To answer the question at hand, how to calculate the average time it takes for a function or code snippet to execute, what I do in the absence of jsPerf is create a test object containing the individual tests I’d like to measure. I then run each test 11 times, recording the last 10 measurements (the first test run acts as a warmup since I notice its time is usually skewed higher). Finally, I divide by 10 to get the average time.

Here’s the code:

// Test setup (code shared by all tests)
var date = new Date();

function startTests() {
  // Define tests
  var oTestSuite = {
    'toString': function() {
      return doTest(function() {
        // Insert code to test below
        // ******************************************************************
        date.toString().split(' ');
        // ******************************************************************
        // End code block
      });
    },
    'concatenate': function() {
      return doTest(function() {
        // Insert code to test below
        // ******************************************************************
        (''+date).split(' ');
        // ******************************************************************
        // End code block
      });
    }
  };
  for (test in oTestSuite) {
    var nTestCount = 10,
      sTotalTime = test + 'Total';
    console.log('Start timing '' + test + '' method...');
    oTestSuite[sTotalTime] = 0;
    oTestSuite[test](); // Warmup
    for (var i=0; i<nTestCount; ++i) {
      var nTime = oTestSuite[test]();
      oTestSuite[sTotalTime] += nTime;
      console.log('Took: ' + nTime + ' ms');
    }
    console.log(test + ' AVERAGE: ' + (oTestSuite[sTotalTime]/nTestCount) +
      'n#################################################################');
  }
}

function doTest(fn) {
  var nStart = performance.now();
  for (var i=0, imax=100000; i<imax; ++i) {
    fn();
  }
  var nStop = performance.now(),
    nTime = nStop-nStart;
  return nTime;
}

Here are the results comparing two string conversion techniques in my Chrome v50 (64-bit):

Start timing 'toString' method...
Took: 204.59500000000003 ms
Took: 210.59000000000003 ms
Took: 205.95500000000004 ms
Took: 206.57000000000005 ms
Took: 204.07500000000005 ms
Took: 205.11000000000013 ms
Took: 203.99500000000012 ms
Took: 213.87500000000023 ms
Took: 222.48499999999967 ms
Took: 206.39499999999998 ms
toString AVERAGE: 208.36450000000005
#################################################################
Start timing 'concatenate' method...
Took: 286.18499999999995 ms
Took: 283.6400000000003 ms
Took: 284.8199999999997 ms
Took: 278.5100000000002 ms
Took: 283.0750000000003 ms
Took: 282.5049999999992 ms
Took: 281.3750000000009 ms
Took: 286.59000000000106 ms
Took: 281.3150000000005 ms
Took: 284.09500000000025 ms
concatenate AVERAGE: 283.21100000000024
#################################################################

You can also play around with it in this pen: http://codepen.io/thdoan/pen/aNKJbQ

Use Js to compute function average running time

Re: The 100 Season 3 Episode 11 Review: Nevermore

As much as I’ve been suspending reality to enjoy The 100, I think I reached my breaking point with this episode. Why don’t they just, you know, gag and blindfold Raven? Or knock her unconscious. Did the entire crew that has managed to survive this long somehow have a brain fart all at once? This gaping hole in logic was too much for me to jump over.

How to knock someone unconcious when there is no pain in the City of Light? I agree on gagging and blindfolding, but that would’ve been a very boring episode, and we wouldn’t have seen another demonstration of Lindsey Morgan’s incredible acting abilities. ;)

Re: The 100 Season 3 Episode 11 Review: Nevermore

Re: GPD Win handheld PC gets a hardware update ahead of launch (crowdfunding)

This might make a nice little media player for in bed or on the couch (where tablets with stands don’t really cut it).

I don’t know about you, but life’s too short to watch movies on a 720p 5.5-inch screen. This could be a passable device to play NES while you’re on the john or in the tube though.

Maybe my standards are low, but I’ve watched a number of movies on my 720p 4.7-inch phone out of necessity and found it to be a good experience. But it’s good to have such options.

Your standards are not low, you guys just have good vision :-).

Re: GPD Win handheld PC gets a hardware update ahead of launch (crowdfunding)

How to handle 403 ajax response in jQuery

Solution: The best I found to deal with this situation per ajax request is to use statusCode method:

statusCode: {
  403: function() { 

  },
  200: function(data) {

  }
  //other codes. read the docs for more details
}

If a server is configured properly, using statusCode will not help you handle 403 errors. Try it out yourself by doing a $.getJSON() on this file (it should return a proper 403 error – “Access Denied”).

If a server is sending a 403 status code in an XMLHTTPRequest response, then the server is misconfigured because per RFC spec a 403 is when the server should not fulfill the request when the requested resource is forbidden, which means you won’t get back a status code.

How to handle 403 ajax response in jQuery

Jquery Ajax call return 403 status

So, handle this error, you can log or notify.

$.ajax({
  type: "POST",
  url: "KeepAliveDummy.aspx",
  success: function (response) {
    //session refreshed
  },
  error: function (xhr, ajaxOptions, thrownError) {
    if(xhr.status==403) {
      //handle error
    }
  }
});

This won’t work because a 403 will trigger neither ‘error’ nor ‘complete’ – your AJAX request is just sorta left hanging. See the reference provided by Hungnh1704 below: “the server is refusing to respond”. Does anyone know how to handle 403 errors?

Jquery Ajax call return 403 status

Re: Is the Meizu Pro 6 a step backwards? Meizu Pro 5 vs Pro 6 specs

Screen size, RAM performance, storage performance, and battery capacity have been downgraded in the Pro 6, so if any of those are important to you, then get the Pro 5 (probably for less $$$ also).

The jury’s still out on CPU, GPS, and Wi-Fi performance, and also heat management to reduce throttling. Also, as balcobomber25 mentioned battery could be a wash since the Pro 5 has a larger display.

Re: Is the Meizu Pro 6 a step backwards? Meizu Pro 5 vs Pro 6 specs

2016 NBA Playoff predictions: Charles Barkley guarantees Golden State Warriors won’t win title – Golden State Of Mind

And I can guarantee you that Charles Barkley won’t ever shut his mouth win or lose, so this isn’t exactly news. Barkley bashing the Warriors – it’s like peanut butter and jam.

2016 NBA Playoff predictions: Charles Barkley guarantees Golden State Warriors won’t win title – Golden State Of Mind