Book Image

Learning Underscore.js

By : Alexandru Vasile Pop
Book Image

Learning Underscore.js

By: Alexandru Vasile Pop

Overview of this book

Table of Contents (14 chapters)
Learning Underscore.js
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Underscore and JavaScript standards


Up until Underscore 1.6, there was a strong correlation between the library and some of the native JavaScript functions available in ES5. Starting with Underscore 1.7, this link was broken and the library introduced implementations that performed better than the native JavaScript equivalents. For example, the native JavaScript functions operating on arrays make provisions for so called sparse arrays. In JavaScript, arrays behave similarly to objects: they can contain elements at arbitrary indexes even if the initial length is less than the index value, as shown in this code snippet:

var arr = [];
arr[5] = null;
arr[12] = 1;
console.log("Array length is " + arr.length);

The array length is now 13, and as it only contains two defined elements, we say that the array is sparse. Prior to Underscore 1.7, enumerating a sparse array using the _.each() function would only touch the valid elements from the array, and this involves extra computational effort. Starting...