Monday, December 7, 2015

Book Review: Learning JavaScript Data Structures and Algorithms

The Packt Publishing book Learning JavaScript Data Structures and Algorithms by Loiane Groner has the subtitle "Understand and implement classic data structures and algorithms using JavaScript." Learning JavaScript Data Structures and Algorithms has just under 200 substantive pages spanning ten chapters in the main book. There is also an additional Chapter 11 and an Appendix referenced in the Preface that can be downloaded as separate PDFs.


The Preface of Learning JavaScript Data Structures and Algorithms briefly describes the importance of data structures and algorithms in software development and provides brief descriptions of each of the book's eleven chapters and of the appendix. In the "What you need for this book" section, the Preface describes three development environments that the reader can choose from to use for examples in the book.

The "Who this book is for" section may be the most important of the Preface I include it here to provide a good idea of who Learning JavaScript Data Structures and Algorithms is best suited for.

This book is intended for students of Computer Science, people who are just starting their career in technology, and those who want to learn about data structures and algorithms with JavaScript. Some knowledge of programming logic is the only thing you need to know to start having fun with algorithms and JavaScript!
This book is written for beginners who want to learn about data structures and algorithms, and also for those who are already familiar with data structures and algorithms but who want to learn how to use them with JavaScript.

Chapter 1: JavaScript – A Quick Overview

The initial chapter of Learning JavaScript Data Structures and Algorithms asserts, "JavaScript is a must-have on your résumé if you are or going to become a web developer." The chapter explains why the author chose JavaScript as the language "to learn about data structures and algorithms."

Chapter 1 of Learning JavaScript Data Structures and Algorithms has a section "Setting up the environment" that describes using a browser such as Chrome or Firefox (with FireBug) as the simplest environment and demonstrates via screen snapshots use of either browser and its associated development tools. This section also describes installing XAMPP and node.js.

Chapter 1's section on "JavaScript basics" begins by describing and demonstrating the two ways (directly embedded and included in referenced file) to include JavaScript in HTML. The chapter then discusses and demonstrates variables and variable scope, types, operators, approaches to writing output, true and false in JavaScript (truthy and falsy), the equals operators, conditional statements, loops, functions, and objects.

The section "Debugging and tools" briefly references JavaScript debugging and recommends the Google Chrome Debugging JavaScript page. This section also recommends choosing an editor from three it lists and briefly describes: Aptana, JetBrains's WebStorm, and Sublime.

I really like Learning JavaScript Data Structure and Algorithms's introduction to JavaScript. The chapter presents JavaScript basics in a clear, concise manner. The author does a nice job of mixing commonly accepted "best practices" of JavaScript development in with the topics as they are introduced. For example, I really like the table that summarizes how true and false work in JavaScript.

Chapter 2: Arrays

Chapter 2 of Learning JavaScript Data Structures and Algorithms introduces the simplest of JavaScript's data structures - arrays. The chapter provides simple code listings and associated explanations that cover creating and initializing arrays, adding elements to and removing elements from arrays, multidimensional arrays, calling methods on arrays, joining arrays, iterating over an array, sorting (including custom sorting) an array, searching with indexOf and lastIndexOf, and writing out the contents of an array as a String with toString() and join(). The second chapter concludes with a short section that lists and briefly describes additional resources on JavaScript arrays.

Chapter 3: Stacks

The third chapter of Learning JavaScript Data Structures and Algorithms introduces the concept of a LIFO stack and how to implement a stack in JavaScript as a custom Stack class. The discussion that introduces the newly written Stack class is followed by discussion on using that class. This chapter is surprisingly short given all the details it introduces (including conversions between different numeric bases) and it concludes with references to the classical "balanced parentheses and the Hanoi tower examples" that can be downloaded with the book's source code (not included in the book itself).

Chapter 4: Queues

FIFO Queues are the subject of Learning JavaScript Data Structures and Algorithms's fourth chapter. As was done in the previous chapter, Chapter 4 begins with discussion and code listings for creating a Queue class and moves onto how to apply this Queue class. The chapter concludes with descriptions of applying a priority queue and Hot Potato queue.

Chapter 5: Linked Lists

Chapter 5 of Learning JavaScript Data Structures and Algorithms introduces linked lists and compares and contrasts linked lists with arrays. As was done in the previous two chapters, the fifth chapter describes and illustrates how to implement its data structure (LinkedList) and then demonstrates using it.

Doubly linked lists are also covered in Chapter 5. The chapter discusses and illustrates adapting LinkedList to be a DoublyLinkedList. This discussion includes several simple graphics and text explaining the complexities of the book-keeping for managing a custom-developer doubly-linked list.

The fifth chapter also introduces the concepts of a circular linked list and a doubly circular linked list. Although the chapter doesn't actually cover the implementation of these data structures, it references the source code that is available for download for those implementations.

Chapter 6: Sets

The data structures covered in Chapters 2 through 5 of Learning JavaScript Data Structures and Algorithms are sequential structures, but the sixth chapter introduces Sets and describes a "Set" as "a collection of items that are unordered and consists of unique elements". The author points out that ECMAScript 6 is expected to provide a built-in Set and then demonstrates implementation of a Set "based on the Set implementation of ECMAScript 6." I found it interesting that a JavaScript Object was chosen to back the custom Set, but the author explains an advantage of this approach: "objects in JavaScript do not allow you to have two different properties on the same key, which guarantees unique elements in our set."

Chapter 6 explains and illustrates with code samples the implementation of a Set with methods matching or similar to those anticipated for the ECMAScript 6 Set. The chapter demonstrates use of the custom Set class. The coverage includes introduction with graphics of different set operations and how to implement them using the Set class.

Chapter 7: Dictionaries and Hashes

Chapter 7 of Learning JavaScript Data Structures and Algorithms introduces two structures that are based on key/value pairs and on the concept of unique entries: dictionaries (maps) and hashes. The chapter states that ECMAScript 6 includes a built-in Map implementation and then proceeds to illustrate how an Object-based Dictionary implementation can be written in JavaScript. As is the pattern in the other chapters on JavaScript data structures, this section also demonstrates use of the custom Dictionary class.

The seventh chapter also illustrates with text and code listings how to implement a HashTable and how to use it. This includes significant discussion on techniques for hash collision resolution.

Chapter 8: Trees

Learning JavaScript Data Structures and Algorithms's eighth chapter begins with an introduction to concepts and terminology associated with trees. The chapter illustrates creation of a BinarySearchTree class and how to traverse it, search it, and perform other basic operations on it. The chapter only briefly introduces the Adelson-Velskii and Landis' (AVL) tree, but states that an implementation of it can be downloaded from the source code associated with the book.

Chapter 9: Graphs

The ninth chapter of Learning JavaScript Data Structures and Algorithms introduces the last major data structure covered in this book: graphs. The chapter starts by introducing graph concepts and terminology and then describes directed and undirected graphs. The chapter explains two common implementations of graphs (adjacency matrix and incidence matrix) before illustrating development of a Graph class. The coverage of graph traversals is significant and detailed.

Chapter 10: Sorting and Searching Algorithms

The last chapter that is included in the PDF version of Learning JavaScript Data Structures and Algorithms that I was provided for my review is focused on common sorting and searching algorithms implemented in JavaScript. The covered sorting algorithms include bubble sort, selection sort, insertion sort, merge sort, and quick sort. I was surprised to learn from reading this book that each browser is free to implement Array.prototype.sort with its own choice of one of these or another sorting implementation.

The second part of the tenth chapter covers searching algorithms implemented in JavaScript and specifically covers sequential search and binary search. Big O notation is important in any discussion of algorithms and their performance and that is covered in more detail in the supplementary chapter and appendix.

Chapter 11: More About Algorithms

This eleventh chapter of Learning JavaScript Data Structures and Algorithms was referenced as a separate download (PDF) in the Preface. This chapter looks in more detail at recursion (introduced in Chapter 8) and Big O notation (introduced in Chapter 10) and discusses dynamic programming and greedy algorithms.

Chapter 11's coverage of recursion describes the situation in recursive code that can lead to stack overflow errors and describes the browser-specific limitations on call stack size. The recursion coverage also introduces ECMAScript 6's support for tail call optimization and revisits a classical recursive example with the Fibonacci sequence.

"Dynamic Programming" (DP) is described in Chapter 11 as "an optimization technique used to solve complex problems by breaking them in to smaller sub-problems." The chapter introduces the three main steps used to solve a DP problem and briefly describes some famous problems solved with DP. Code and text explanations cover implementing a solution for the minimum coin change problem.

The "Greedy Algorithms" section of the eleventh chapter states that a greedy algorithm "makes the locally optimal choice (the best solution at the time) at each stage with the hope of finding a global optimum (global best solution)." It then approaches the same minimum code change problem solved with dynamic programming approach with the greedy algorithm implementation and compares and contrasts the two approaches.

The supplementary Chapter 11 provides more in-depth discussion of Big O notation that includes a nice, simple table of the various notations and their corresponding names before describing each notation in more detail. There's a nice color chart comparing the complexities of the different levels of Big O notation and I like that the source code for generating that chart in JavaScript can be downloaded with the source associated with the book.

Chapter 11 concludes with a listing and brief description of online sites that allow developers to "[have] fun with algorithms".

Appendix: Big-O Cheat Sheet

Like Chapter 11, the Appendix is not included in the main book (at least in the PDF version I reviewed), but is instead available as a separate download (PDF) of two pages. This appendix begins with a table summarizing "the big-O notation for the insert, delete, and search operations of the data structures" covered in Learning JavaScript Data Structures and Algorithms. Similar tables exist for graph operations, sorting algorithms, and search algorithms.

General Observations

  • Learning JavaScript Data Structures and Algorithms provides an excellent introduction to data structures and to implementing and using common data structures in JavaScript. As such, it is a book that should appeal and be of use to a developer with minimal or no JavaScript experience or even someone who is not necessarily interested in JavaScript, but is interested in learning about data structures.
    • JavaScript turns out to be a pretty good language to teach and learn data structures. It's an added bonus that many of the generated data structures can be used in JavaScript code without being redundant with built-in language data structures. One of the drawbacks of teaching data structures with Java, for example, is that it's hard for the learner to see the benefit when he or she knows Java already has a rich collection of data structures.
    • Although the book is, at its title suggests, on JavaScript data structures, much of the content is largely applicable to many modern programming languages. There is clear, concise coverage of common data structures, issues to consider when implementing and using them, and the relative costs of operations (Big O notation) associated with these structures.
  • There are several screen snapshots included in Learning JavaScript Data Structures and Algorithms that are clear resolution and in color. Some of these are annotated to make them even easier to understand quickly.
  • The are several simple graphics included in Learning JavaScript Data Structures and Algorithms that are black symbols on white background. Although very simple, they communicate the information and concepts well. Indeed, their simplicity likely increases their communicative value even if they are not the most aesthetically pleasing. Given their simple nature, they probably appear very clearly even in a printed book with black text and white pages.
  • Code listings in Learning JavaScript Data Structures and Algorithms are black font on white background (no color syntax) without line numbers in the PDF version. For greater code readability, I recommend downloading the source code for this book from the Packt site and opening the code in your favorite text editor (such as Sublime) or IDE.
    • It is a common characteristic of Packt books to not include line numbers in code listings. However, Learning JavaScript Data Structures and Algorithms does place markers such as {1} after comment slashes (//) on certain lines that are later described in conjunction with that line number marking. This functions similarly to having line numbers on the source code in terms of making it easier to see clearly what lines of code are being referenced specifically in the discussion text.
    • It is recommended to download the code associated with the book not only to read it in your favorite editor, but because many included supplementary examples were mentioned but not shown in the book.
  • Several of the Packt Publishing books I've read and/or reviewed have significant editing issues such as numerous typos and misspellings. I was impressed, however, with the polish of Learning JavaScript Data Structures and Algorithms. It not only avoids typos and misspellings, but features easy-to-read prose and reads very quickly.


I have reviewed numerous Packt Publishing books, but feel comfortable stating that Learning JavaScript Data Structures and Algorithms is one of the best Packt Publishing titles I've read (probably in the top five). Learning JavaScript Data Structures and Algorithms is both an excellent introduction to JavaScript and to writing and using common data structures and algorithms in JavaScript. Perhaps what surprised me most about this book is that it would be a good introduction to data structures for anyone interested in learning about data structures regardless of their preferred programming language.

1 comment:

Loiane said...

Thank you for reviewing the book, and I'm really happy that you liked it!