GUIDES Optimizing Embedded JavaScript Know Where to Optimize
About
Binary Data
Index
Convert String to ArrayBuffer
Convert ArrayBuffer to String
Convert ArrayBuffers to String
Handle Errors Converting ArrayBuffer to String
Immutable ArrayBuffers
Resize an ArrayBuffer
Combine ArrayBuffers
Convert Base64 to Binary Data
Convert Binary Data to Base64
Convert Binary Data to Hex
Convert Hex to Binary Data
Calculate CRC for Binary Data
Compress Binary Data – One Buffer
Compress Binary Data – Streaming
Decompress Binary Data – One Buffer
Decompress Binary Data – Streaming
Callbacks
Index
One-Time Callback
Repeating Callback
Repeating Callback with Initial Delay
Immediate Callback
Reschedule Callback
Cancel Callback
Suspend Callback
What About setTimeout?
Optimizing Embedded JavaScript
Index
When to Optimize
Know Where to Optimize
Looping through an Array
Iterating Over a String
Building a String
Avoid Copying Buffers
Accessing Properties
Map versus Object
Appending to an Array
Operating on Bits
Defining Class Methods
Reducing Stack Use
Time
Index
Get Unix Time
Get Time of Day
Get Date
Get Time Since System Start
Get Microseconds
Set System Date and Time
Set Real-Time Clock Time
Get Time and Date from Network
Get Time and Date from Real-Time Clock
Sleep

Know Where to Optimize

It's usually easy to know that code isn't running fast enough. You can feel it. Determining why the code is running slowly can be difficult. You may have an intuition about what's making things slow. Wouldn't it be nice to know for sure, before taking the time to optimize the code?

Fortunately, there's a tool to help. There's a performance profiler built into the xsbug JavaScript debugger. It measures the performance of JavaScript running on embedded devices and in the desktop simulator. You don't need any special set-up to run it – just click start and watch the results roll in.

We've got a couple of articles to get you started with the profiler.

Sometimes you may not see an immediate solution to the performance hotspot because most of the time is being taken by a system library, rather than your code. There's no single solution, but there are some good places to start looking.

  • If the garbage collector is taking a significant amount of time, you have a couple of options. First, you may be able to modify your code to allocate less memory. See Avoid Copying Buffers and Building a String, for example.
  • If a particular function is slow, sometimes an easy option is to call it less frequently. Alternatively, check the documentation as the function may have options that can lower its overhead, such as the push() method of arrays.