Note! This article is part of a series on Mobile development and JavaScript (see here). In our previous installment Javascript Tools -- Syntax and String Testing, we created a small program "person.js". See below for listing.

Now lets run a performance test. At a JavaScript presentation I attended, a member of the audience suggested use of try/catch effects performance. I've done a lot work in industries where fine-grained exception handling (including identification) is pretty standard. That means, it's usually unacceptable to implement a business function and not trap unexpected run time errors. So for me, use of a try/catch block is standard. So let's use our tiny application and test the performance. Let's see if adding a try/catch impacts performance.

Lets use A really nice service and important tool.

We'll take our person function and instantiate joe (see above). That's the test setup.

We'll create 2 test cases. 1) We'll get the first name with out using try/catch. 2) We'll get the last name, but we'll enclose the statements is a try/catch block.

Test 1 is titled "No try/catch" :

var firstName = null;
firstName = joe.getFirstName();

Test 2 is titled "With try/catch" :

var lastName = null;
try {
    lastName = joe.getLastName();
} catch(error) {
    console.log("An error occurred");

Our setup code, as mentioned before is our person.js

var person = function(first_name,last_name) {
    this.init = function(first_name,last_name) {
        if (first_name) {
            this.first_name = first_name;
        if (last_name) {
            this.last_name = last_name;
    this.getFirstName = function() {
        return this.first_name;
    this.setFirstName=function(first_name) {
        this.first_name = first_name;
    this.getLastName = function() {
        return this.last_name;
    this.setLastName= function(last_name) {
        this.last_name = last_name;

Js Perf allows does not require not require you to publish your test immediately. Js Perf validates your test suite. Thus you can tweak your test before publishing. Again, just really great tool.

I ran our short test suite and published results here:

The other great thing about is, you can access their website with your mobile web browser and repeat the tests. FYI different browsers have different JavaScript engines (AKA loaders, virtual machines).

Preliminary results are try/catch is not a performance factor. However, those results are preliminary and perhaps the test it self can be improved. Technology changes, perhaps in a previous version of JavaScript inclusion of a try/catch block slowed down a program? Personally, I doubt that, but that why we test :)

To summarize, if we come to performance critical section of our application. We can use a performance testing tool like JS PERF to collect some empirical data.

About the Author:
Lorin M Klugman is an experienced developer focused on new technology.
Open for work (contract or hire).
Drop Lorin a note. Click here for address.
Please no recruiters :)
- Home