GUIDES Optimizing Embedded JavaScript When 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

When to Optimize

As a rule, don't spend much time optimizing when you're starting a project. Often optimized code is more complex and therefore more difficult to debug. You don't want that getting in your way while you're still getting your project to work.

Do take some time to learn about common optimization techniques. If you can easily apply them when writing your code, do that. Some of the techniques will become second nature.

Reserve your optimization effort for those parts of your code where it really matters. If a few lines of code run once at initialization, maybe they don't need to be as efficient as possible (unless, of course, your project takes too long to start up). Code that runs inside a loop tends to be more important to optimize.

Optimization is usually about getting performance to the point where it is "good enough." The definition of "good enough" depends on your project's goals. Code that is sufficiently optimized for one project may need to be tuned for another.

Optimization isn't only about performance. Many other aspects of an embedded system may need to be optimized, including memory, system resources, energy, and code size. These are not independent. For example, there is often a tradeoff between performance and memory use.