Book Image

Hands-On Data Structures and Algorithms with JavaScript

By : Kashyap Mukkamala
Book Image

Hands-On Data Structures and Algorithms with JavaScript

By: Kashyap Mukkamala

Overview of this book

Data structures and algorithms are the fundamental building blocks of computer programming. They are critical to any problem, provide a complete solution, and act like reusable code. Using appropriate data structures and having a good understanding of algorithm analysis are key in JavaScript to solving crises and ensuring your application is less prone to errors. Do you want to build applications that are high-performing and fast? Are you looking for complete solutions to implement complex data structures and algorithms in a practical way? If either of these questions rings a bell, then this book is for you! You'll start by building stacks and understanding performance and memory implications. You will learn how to pick the right type of queue for the application. You will then use sets, maps, trees, and graphs to simplify complex applications. You will learn to implement different types of sorting algorithm before gradually calculating and analyzing space and time complexity. Finally, you'll increase the performance of your application using micro optimizations and memory management. By the end of the book you will have gained the skills and expertise necessary to create and employ various data structures in a way that is demanded by your project or use case.
Table of Contents (16 chapters)
Title Page
Copyright and Credits
PacktPub.com
Contributors
Preface
5
Simplify Complex Applications Using Graphs
Index

Preface

The main focus of this book is employing data structures and algorithms in real-world web applications using JavaScript.

With JavaScript making its way onto the server side and with Single Page Application (SPA) frameworks taking over the client side, a lot, if not all, of the business logic, is being ported over to the client side. This makes it crucial to employ hand-crafted data structures and algorithms that are tailor-made for a given use case.

For example, when working on data visualizations such as charts, graphs, and 3D or 4D models, there might be tens or even hundreds of thousands of complex objects being served from the server, sometimes in near real time. There are more ways than one in which this data can be handled and that is what we will be exploring, with real-world examples.

Who this book is for

This book is for anyone who has an interest in and basic knowledge of HTML, CSS, and JavaScript. We will also be using Node.js, Express, and Angular to create some of the web apps and APIs that leverage our data structures.

What this book covers

Chapter 1Building Stacks for Application State Management,  introduces building and using stacks for things such as a custom back button for an application and a syntax parser and evaluator for an online IDE.

Chapter 2Creating Queues for In-Order Executions, demonstrates using queues and their variants to create a messaging service capable of handling message failures. Then, we perform a quick comparison of the different types of queues.

Chapter 3Using Sets and Maps for Faster Applications, use sets, and maps to create keyboard shortcuts to navigate between your application states. Then, we create a custom application tracker for recording the analytics information of a web application. We conclude the chapter with a performance comparison of sets and maps with arrays and objects.

Chapter 4, Using Trees for Faster Lookup and Modifications, leverages tree data structures to form a typeahead component. Then, we create a credit card approval predictor to determine whether or not a credit card application would be accepted based on historical data.

Chapter 5, Simplify Complex Applications Using Graphs, discusses graphs with examples such as creating a reference generator for a job portal and a friend recommendation system on a social media website.

Chapter 6, Exploring Types of Algorithms, explores some of the most important algorithms, such as Dijkstra's, knapsack 1/0, greedy algorithms, and so on.

Chapter 7,  Sorting and its Applications, explores merge sort, insertion sort, and quick sort with examples. Then, we run a performance comparison on them.

Chapter 8, Big O notation, Space, and Time Complexity, discusses the notations denoting complexities and then, moves on to discuss what space and time complexities are and how they impact our application. 

Chapter 9, Micro-optimizations and Memory Management, explores the best practices for HTML, CSS, JavaScript and then, moves on to discuss some of the internal workings of Google Chrome and how we can leverage it to render our applications better and more quickly.

To get the most out of this book

  • Basic knowledge of JavaScript, HTML, and CSS
  • Have Node.js installed (https://nodejs.org/en/download/)
  • Install WebStorm IDE (https://www.jetbrains.com/webstorm/download) or similar
  • A next-generation browser such as Google Chrome (https://www.google.com/chrome/browser/desktop/)
  • Familiarity with Angular 2.0 or greater is a plus but is not required
  • The screenshots in this book are taken on a macOS. There would be little difference (if any) for users of any other OS. The code samples, however, would run without any discrepancies irrespective of the OS. Anywhere we have CMD/cmd/command specified, please use CTRL/ctrl/control key on the windows counterpart. If you see return, please use Enter and if you see the term terminal/Terminal please use its equivalent command prompt on windows. 
  • In this book, the code base is built incrementally as the topic progresses. So, when you compare the beginning of a code sample with the code base in GitHub, be aware that the GitHub code is the final form of the topic or the example that you are referring to.

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Practical-JavaScript-Data-Structures-and-Algorithms. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here:https://www.packtpub.com/sites/default/files/downloads/HandsOnDataStructuresandAlgorithmswithJavaScript_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "The native array operations have varying time complexities. Let's take Array.prototype.splice and Array.prototype.push."

A block of code is set as follows:

class Stack {
    constructor() {

    }
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

var express = require('express');
var app = express();
var data = require('./books.json');
var Insertion = require('./sort/insertion');

Any command-line input or output is written as follows:

ng new back-button

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "When the user clicks on the back button, we will navigate to the previous state of the application from the stack."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.