Faster JavaScript: From responsive designs to minimal JSON

Feature:

Making JavaScript faster: From responsive designs to minimizing JSON

By Jason Tee

TheServerSide.com

JavaScript and JavaServer Pages (JSPs) are the workhorses of enterprise web development in Java, getting all those tedious little tasks done that keep everything else running smoothly. Without them, there’s no dynamo in your dynamic content. Using these technologies properly can mean the difference between a fast running river of highly performant code and a mess of buggy molasses. Here’s some current advice from around the JavaSphere that may help you improve the way you use JavaScript (JS) and JSPs.

If you like your JavaScript to look elegant while getting the job done, you won’t want to miss these tips.

Jason Tee, Enterprise Software Architect

Smart JavaScript techniques and patterns

This timely tutorial at onextrapixel.com is all about functionality in how you actually write your code. If you like your JavaScript to look elegant while getting the job done, you won’t want to miss these tips. For example, do you know when to replace logical operators and comparisons with a simple regex test? This tutorial will help you to find out how. If you aren’t very familiar with the use of regular expressions, you can get started here.

Creating responsive designs

Don’t you hate it how consumer trends keep swinging back and forth from the micro to the macro? First, everyone wants the slimmest or most compact smartphones. Now, Apple is apparently creating a whopping 12” iPad. The larger the device, the more stuff it can do. That’s fine and dandy for the end user, but trying to create apps that work across such a range of device types and screen resolutions can be a real beast for developers. It becomes necessary to make some tough decisions about what to keep and what to squeeze out when the app is being deployed on a smaller device. Using JavaScript to detect the screen size and then having it update and apply the appropriate CSS styles is one way to implement a smart solution. The Bootstrap library is the most popular pick for this purpose. Foundation isn't bad either. You can see more options here.

Minimizing JSON

What do Java developers do on a winter Sunday? Some of them like to parse JSON. Ralf Sternberg, a committer and project lead for Eclipse RAP, put together a fast and lightweight library with just ten classes. Apparently, going lean with the parser actually improves server performance because "the server processes and creates JSON messages for a large number of clients at a high rate." There are no dependencies on an external JSON library, the code is easy to maintain, and it’s not a memory hog. It isn’t going to be enough for all your JSON projects, but it does a few things well. You can find out more about usage for this mini-library here.

Writing memory efficient code

This article on Smashing Magazine is chock full of goodies for developers coding apps that will run on Google’s V8 or IE’s Chakra, etc. There’s apparently a lot going on behind the scenes with this type of JS engine. It’s important to know what these engines are doing for you. What are they optimizing and what are they unable to do? Is the garbage collector able to pick up the slack and keep things clean?

Here are just a few of the tips provided by Addy Osmani:

  1. Go local, not global. Help your garbage collector out by using a function-local variable that goes out of scope when it’s no longer needed.
  2. Clean your local data cache regularly or use an aging mechanism to do it for you so there are no big chunks of data lodged there that you aren’t going to reuse.
  3. If you want to store a passel of numbers or a list of objects of the same type, an array is the best option.

Get more advice along with details and examples here.

This educational piece by khoomeister on GitHub Gist has a little something for everyone. Some of the advice is stuff you already know but need to have knocked into your head again (e.g., avoid premature optimization). But you might also learn a few tricks like using iife rather than doSomething so you get one privateHelper rather than recurring nestedHelpers. This is the place to go to brush up fast on the stuff you do every day in JavaScript. Yes, sometimes it stings a little to realize how long you’ve been doing it wrong. But doing it right makes life so much easier! To find more jsPerf benchmark guidelines, click through.

What are your favorite JS and JSP tips and tricks? Let us know your secrets.

11 Aug 2013

Related Content

Related Resources