Node.Js High Performance
Total Page:16
File Type:pdf, Size:1020Kb
www.allitebooks.com Node.js High Performance Take your application to the next level of high performance using the extensive capabilities of Node.js Diogo Resende BIRMINGHAM - MUMBAI www.allitebooks.com Node.js High Performance Copyright © 2015 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: August 2015 Production reference: 1120815 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78528-614-8 www.packtpub.com www.allitebooks.com Credits Author Copy Editor Diogo Resende Vikrant Phadkay Reviewers Project Coordinator Abhishek Dey Judie Jose Glenn Geenen Stefan Lapers Proofreader Safis Editing Aravind V.S Indexer Commissioning Editor Rekha Nair Ashwin Nair Production Coordinator Acquisition Editor Manu Joseph Sonali Vernekar Cover Work Content Development Editor Manu Joseph Rashmi Suvarna Technical Editor Utkarsha S. Kadam www.allitebooks.com About the Author Diogo Resende is a passionate developer obsessed with perfection in everything he works on. He loves everything about the Internet of Things, which is the ability to connect everything together and always be connected to the world. He studied computer science and graduated in engineering. At that time, he deepened his knowledge of computer networking and security, software development, and cloud computing. Over the past 10 years, Diogo has embraced different challenges to develop applications and services to connect people with embedded devices around the world, building a bridge between old and uncommon protocols and the Internet of today. ThinkDigital has been his employer and a major part of his life for the last few years. It offers services and expertise in areas such as computer networking and security, automation, smart metering, and fleet management and intelligence. Diogo has also published many open source projects. You can find them all, with an MIT license style, on his personal GitHub page under the username dresende. First of all, I would like to thank my wife, Ana, for putting up with my late-night writing sessions. She has given me enough of the space and tranquility that I needed to take up this challenge. I would also like to thank my son, Manuel, for being born exactly when I started writing the book, for stealing my attention but also making my days happier, and for giving me the strength to carry on and overcome every obstacle. Last but not least, I would like to thank everyone in my company for putting up with me. I thank my business associate, Nuno, and my work colleagues Sílvia, Luis, and Helder for collaborating and helping the company go ahead and achieve all our dreams. www.allitebooks.com About the Reviewers Abhishek Dey was born in Bandel, West Bengal, India. He holds an MS degree in computer engineering from the University of Florida, Gainesville, USA. His research interests lie primarily in the fields of compiler design, computer security, networks, data mining, analyses of algorithms, and concurrency and parallelism. He is a passionate programmer, who started programming in C and Java at the age of 10. Shortly afterwards, he developed a strong interest in web technologies and system implementation. Abhishek possesses profound expertise in developing high-volume software using C++, Java, C#, JavaScript, jQuery, AngularJS, and HTML5. He also enjoys coding in functional programming languages, such as SML. Some of his recent projects can be found at https://github.com/deyabhishek. He is a Microsoft Certified Professional, an Oracle Certified Java Programmer, an Oracle Certified Professional Java EE Web Component Developer, and an Oracle Certified Professional Java EE Business Component Developer. In his leisure time, Abhishek loves to listen to music, travel to interesting places, and paint something on canvas, giving colors to his imagination. More information about him can be found at http://abhishekdey.com. He has reviewed Kali Linux CTF Blueprints, AngularJS UI Development, RESTful Web API Design with Node.js, and Mastering AngularJS for .NET Developers, all by Packt Publishing. Glenn Geenen is a Node.js developer with a background in game and mobile development. He worked mostly as an iOS consultant before becoming a Node.js consultant for his own company, GeenenTijd. www.allitebooks.com Stefan Lapers started his career almost 20 years ago as an IT support engineer. Then, he quickly grew in the field of Linux/Unix system engineering and software development. Over the years, he has gained experience in deploying and maintaining hosted application solutions while working for prominent customers, such as MTV, TMF, and many more. In recent years, Stefan was involved in multiple development projects and their delivery as services on the Internet. In his spare time, he enjoys being with his family and flying remotely controlled helicopters. Aravind V.S is an aspiring mind and a creative brain to look forward to in the field of technology. He is a successful entrepreneur, developer, and technology consultant whose interest in embedded systems and computers paved his way into the programming world at the age of 15. At that time, he developed a full-fledged stock and inventory management system for a family friend. He has cofounded Entity Business Foundations, a web and mobile technology start-up based in Kerala (https://teamebf.com/); founded ioStash, an open source Internet of Things platform (http://iostash.com/); and tailored cloud:VAR, an open source backendless web application framework (http://cloudvar.org/) written in NodeJS and MongoDB. In his spare time, Aravind can be found outdoors, focusing his camera, reading books, or writing articles for his blog at http://aravindvs.com/blog/. He has previously reviewed NodeJS Cookbook and NodeJS Essentials by Packt Publishing. Currently, he works as the chief technology officer at Entity Business Foundations. You can contact him at [email protected]. I would like to take this opportunity to thank my friends— Harikrishnan, Abdulla Ahsan, and Muhammed Anas—and my parents for their support in completing the review of this book. Thanks especially to my best friend, Kavya Babu, for her enduring support, encouragement, and faith in me, without which I wouldn't have been what I am today. Above all, I'd like to thank the Almighty for giving me everything I needed at the right time. www.allitebooks.com www.PacktPub.com Support files, eBooks, discount offers, and more For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. TM https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books. Why subscribe? • Fully searchable across every book published by Packt • Copy and paste, print, and bookmark content • On demand and accessible via a web browser Free access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access. www.allitebooks.com www.allitebooks.com Table of Contents Preface v Chapter 1: Introduction and Composition 1 Performance analysis 2 Monitoring 3 Getting high performance 4 Testing and benchmarking 5 Composition in applications 6 Using NPM 7 Separating your code 7 Embracing asynchronous tasks 8 Using library functions 9 Using function rules 9 Testing your modules 10 Summary 11 Chapter 2: Development Patterns 13 What are patterns? 13 Node.js patterns 15 Types of patterns 16 Architectural patterns 16 Creational patterns 21 Structural patterns 23 Behavioral patterns 25 Event-driven architecture 27 Streams 28 Buffers 29 Optimizations 29 Hidden types 30 Numbers 30 Arrays 31 [ i ] www.allitebooks.com Table of Contents Functions 31 The for-in loops 32 The infinite loops 32 The try-catch blocks 32 Eval 32 Summary 33 Chapter 3: Garbage Collection 35 Automatic memory management 35 Memory organization 37 Memory leaks 38 Event emitters 39 Referencing objects 40 Object representation 42 Object heaps 42 Heap snapshots 43 Third-party management 54 Summary 54