Java S cript:
Optimizing Native JavaScript
Designing, programming, and debugging
native JavaScript applications
Using JavaScript, HTML, and CSS
in real projects with real deadlines and real constraints
Includes tips on UI design, optimization, and interviewing
M ir a V ista P ress
by
Robert C. Etheredge
JavaScript: Optimizing Native JavaScript
Copyright 2017 by MiraVista Press
Published by MiraVista Press
All rights reserved. No part of this book may be duplicated in any manner whatsoever without the express written consent of the publisher, except in the form of brief excerpts or quotations used for the purposes of review.
ISBN: 978-0-9863076-5-2
Library of Congress Control Number: 2017901089
MiraVista Press
P.O. Box 961
Orinda, CA 94563
925-253-9472
Email: support@miravista.com
Web: http://www.miravista.com
http://www.nativeJavaScript.com
Printed in the United States of America.
About the Author
Robert C. Etheredge is the founder of MiraVista Press and myWriterTools. He has been programming for over 30 years. He wrote his first programs on IBM punch cards, graduated to using paper tape programming U.S. Navy computers, and wrote his first commercial software on 5 1/4" Apple II floppy disks. He has designed or programmed more than 20 software products, and spent 16 years programming set top boxes and mobile devices using JavaScript and HTML. Recently he has been designing and programming JavaScript/HTML interfaces to process big data for health related applications.
DEDICATION
This book is dedicated to my children, and to my wife Cheri who has stood by me and put up with me for all these years.
"There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies."
- C.A.R. Hoare
Introduction
Writing applications using JavaScript, HTML, and CSS can be fun and rewarding. JavaScript is a fairly easy language to learn and HTML/CSS makes it easy to create great looking interfaces without a lot of graphics experience. It is easy and inexpensive to get your own website up and running, or to make great apps for mobile devices. There is a large developer community online with many libraries and frameworks that you can download to help your development efforts.
However, this same low barrier to entry also makes it easy for bad JavaScript apps to be written. And by bad, I don't mean just inefficient code, but also apps that involve bad design and framework decisions. And this criticism is directed mainly at single-page, multi-screen JavaScript applications, and not just simple web pages. There are a number of reasons for these problems, including:
Novice programmers who have little or no training or experience in programming.
Designers and programmers coming from page-oriented web-site design.
Experienced C++ and Java programmers that don't really understand how best to write JavaScript applications.
Fast desktop computers with lots of memory that hide inefficiencies in code that may have to run on less powerful computers and mobile devices.
Why write about native JavaScript?
Many JavaScript websites and applications use any number of libraries and frameworks along with their native JavaScript code. In many cases, their use can shorten the development cycle and make the code easier to maintain by large development groups. However, I have found when writing high performance applications or highly customized applications that these libraries and frameworks often cause more trouble than they are worth. They add to the size of the code, slow things down, and often make maintenance harder because of having to track versions. Often, you will find a library to carry out a specific graphic or UI function only to find when you are almost done that it does not do exactly what you need and may be more trouble than it is worth to change it. Once modified, you no longer have a standard plug-in but rather customized code that you have to maintain. So this book concentrates on helping you write smaller and faster native JavaScript code.
There are lots of books about the many libraries and frameworks available for JavaScript development. If you develop websites and applications in JavaScript you most likely will use one or more of them in the course of your career. But remember that these libraries and frameworks are all themselves written in JavaScript and share all of the strengths and weaknesses pointed out in this book. Your knowledge of native JavaScript can only help you in your efforts.
Target audience of this book
Javascript: Optimizing Native JavaScript assumes that you have a good working knowledge of writing JavaScript applications or webpages using JavaScript, HTML, and CSS. You need to understand how those three systems interact and how to structure common applications. This book is not a language reference but more a collection of information to help you write more efficient JavaScript programs. Even if your application requires the use of frameworks and libraries, knowing how to code more efficient JavaScript will still be important. In these cases, you can still use frameworks and libraries for the majority of the code but native JavaScript where performance is important.
Based on real-life experience
I have been designing and programming JavaScript applications for more than 17 years. Most of this work was on applications designed for low memory and slow CPU platforms, such as set-top boxes and mobile devices. I found that many recommended solutions didn't necessarily make sense in real life situations. When you work on producing applications on deadlines, you tend to have different restrictions, methods, and goals.
Through years of examining JavaScript code, I discovered repeating conditions that led to the JavaScript/HTML code running poorly on the targeted platforms. These conditions included excessive number of objects, unnecessary date manipulation, excessive error checking, creation of excess garbage, using inefficient libraries, and poorly designed data structures. In many cases, I was able to reduce code size by up to 60% while easily doubling the speed of the code.
Write your code for others
Remember---someone else will very likely have to maintain or decipher your code. Or you may have to read someone else's code and hopefully be able to reuse it instead of starting over again. The clearer and simpler the code is written, the more likely it is to be reused---and this saves time and money.
Assume that whoever will be trying to make sense of your code does not know as much as you do. Be careful if you write code whose function is not obvious or could be ambiguous. If you are using some esoteric feature of JavaScript or some pattern that requires reading programming references to figure out how to use it---you probably shouldn't. There are always good reasons to use some of these advanced methods if your program requires it---just make sure you adequately document the code with clear in-line comments.
JavaScript, with its global namespace and interpreted functionality, is just different than most other languages. And writing code for companies that must meet real deadlines and have compact code that must perform on a variety of platforms is always a challenge.
Why is this book different from other books on JavaScript?
This is not a JavaScript reference book. It is not about using the latest frameworks and libraries. It does not cover esoteric coding solutions and algorithms, nor do I get into the inner workings of the JavaScript engine. This book assumes you already know JavaScript at some level and you can at least program simple pages. For more advanced JavaScript programmers, there are a number of good reference books available.
Next page