JAVA SCRIPT - Performance Testing Different Coding Techniques

Performance Testing Different Coding Techniques



Problem

In JavaScript there are, typically, multiple ways you can code a solution. The problem then becomes determining which of the different ways performs best across different environments.

Solution

One approach is to use a performance testing tool, such as jsPerf, to try out the different approaches and then proceed accordingly. For instance. I wanted to determine which had better performance—using an anonymous function or a named function—when passed as a callback function in an Array method. In jsPerf, I set up an array of string elements and created the named function, rpl(), in the Preparation code section:


var charSet = ["**","bb","cd","**","cc","**","dd","**"];
function rpl (element) {
 return (element !== "**");
};

My first test case was using the anonymous function:

var newArray = charSet.filter(function(element) {
 return (element !== "**");
});

My second test case was using the named function:

var newArray = charSet.filter(rpl);

EXPLAIN

There are variations of performance testing, from the simple alternative testing demon‐ strated in the solution, to complex, involved load testing of entire systems. These types of testing aren’t used to discover whether there are bugs in the code, or if the code meets use requirements—unit testing should find the former, and some form of user compli‐ ance testing finds the latter. Performance testing is specifically for finding the best, most efficient approach to cre‐ ating your application, and then making sure it meets the demands placed on it when running at peak usage.  

Another approach to performance testing is profiling. Most browser debuggers have a built-in profiling capability. As an example, the popular Firebug debugger for Firefox has profiling built in and available with the click of the “Profile” button, 

Once you turn profiling on, you can run your user compliance tests as a way of generating good usage statistics, and then click the “Profile” button again. Firebug then generates a listing of functions called any time for them to respond.

Chrome also has extensive profiling capability, . To use it, open up the JavaScript Console, click the Profiles tab, and then start whichever profiling type you want to start. After you’ve used your application for some time, click the Profiles “Stop” button and view the results.


0 comments:

Post a Comment