More than ten years have passed since Douglas Crockford released his first book about JavaScript. It’s now time to embrace a new one. It’s supposed to shed a light on different aspects of the language, point out the problems and discover possible solutions.
Who is Douglas Crockford
If you’ve been in the JavaScript world for a while, you must know Douglas Crockford. At least you most probably have seen his name in some programming book or an article. He is one of the JavaScript pioneers, who wrote thousands of lines of production code when we all still kids. He is an active contributor to JavaScript standards, tools and related web technologies, as well as an author of JSON format.
Back in 2008, Douglas Crockford wrote a book called “JavaScript: The Good Parts”, which became quite popular among web developers. But time goes fast and especially in the frontend world – it’s flying with the speed of sound. After about 10 years, another book by this author has recently been released. It’s called “How JavaScript works”.
I was lucky to attend the talk by Mr Crockford at Codemotion Amsterdam 2019, where he presented his new book to the web community. The talk was in a format I’ve never seen before: the speaker was literally reading his book to the audience. He prepared a few interesting slides and the talk mostly consisted of two chapters of the book, mixed with Q&A sessions.
The fun fact is that the book’s chapters and pages start with the number zero and it took some effort to manage this with a publishing company. As the author says: “The book is written by a programmer for programmers”. You can clearly see the chapters list is formatted in our lovely JSON:
Chapter 2: How Numbers Work
After a short introduction, the author started with the third chapter, which, as you remember, is numbered as 2 (two). The chapter is called “How Numbers Work” and is fully dedicated to the different types of numbers and how JavaScript handles them.
When web developers start talking about numbers and caveats around them, there is one example which normally pops up in their minds:
The problem here is not with JavaScript itself, but how computers deal with floating numbers in general. Internally, they use a binary format which can not represent floating numbers accurately. That’s why this problem exists in almost every programming language.
Chapter 2 is supposed to shed a light on the fundamental things related to numbers and remind us how extremely important it is to understand how numbers work under the hood. JavaScript handles floating point numbers according to IEEE Standard for Floating-Point Arithmetic (IEEE 754) standard, where any number stored in computer’s memory is a combination of three different parts: sign, significand (also called “mantissa”) and exponent. In general, the standard “IEEE 754” defines two formats: single and double precisions:
JavaScript uses the double precision format only though. So that means every instance of Number uses 64 bits of memory.
According to the table, the significand part is limited by 52 bits and, in case it exceeds this number, the rest simply get cut off. Here is the root cause of the problem with floating numbers.
It’s just one thing among all possible issues when you work with numbers. Chapter 2 of the book should help us understand better potential problems and find solutions to solve them.
Chapter 24: How Optimisation Works
When Mr Crockford finished answering all the questions related to the third chapter, he switched to chapter 24 (twenty-four) and read some excerpts from it. In this chapter, he shows some good practices of writing a “fast” code and shares some bad examples of code we should avoid.
When we start optimising our code or application, the first thing we have to do is to find the root cause of our problem. Or, in case we want to increase the speed, we need to determine a bottleneck – the place which slows down the application and can be improved. Once we know what exactly we want to optimise, it’s much easier to find the right solution.
Another very important thing here is measurement. How do we know if our next update/release/hotfix will improve the current implementation? We need to measure the performance correctly and do it before and after our changes. The measurement here is a key! Only if we compare measurement results of before and after state we can be sure that we’re really optimising our application and not making it worse.
After the presentation, I felt like I wanted to buy the book. Mr Crockford is an extremely experienced programmer, his new book seems to touch upon serious questions and deeply analyse them. It looks very promising and I am, as a JavaScript developer, planning to read all the chapters.