【转】The magic behind array length property

Developer deals with arrays every day. Being a collection, an important property to query is the number of items: Array.prototype.length
In JavaScript the length does not always indicate the number of existing elements (for sparse arrays) and modifying this property may remove elements. 
Let‘s demystify the magic behind this property.

Definition 
The length of an array is an unsigned, 32-bit integer that is numerically greater than the highest index in the array.

This property behaves differently for specific array types. Let‘s enumerate them: 
An array is dense when it‘s elements have contiguous indexes starting at 0. For example [1, 3, 4] is dense, because the indexes are contiguous: 01 and 2
An array is sparse when it‘s elements don‘t have contiguous indexes starting at 0. For example [1, , 4, 6] is sparse, because elements indexes are not contiguous: 02 and 3.

Length as the number of elements in array

The common usage of the length is to determine the number of elements. This is correct for dense collection type:

var fruits = [‘orange‘, ‘apple‘, ‘banana‘]; //fruits is a dense array
fruits.length // prints 3, the real count of elements

fruits.push(‘mango‘);
fruits.length // prints 4, one element was added

var empty = [];
empty.length // prints 0, empty array

See the example in JS Bin

The dense array does not have empties and the number of items corresponds to highestIndex + 1. In [3, 5, 7, 8] the highest index is 3 of element 8, thus the array size is 3 + 1 = 4.

Length as a number bigger than highest index

In a sparse array the length is greater than the highest index, but it does not indicate the real number of elements. When querying length, it‘s bigger than elements count. It happens because of the gaps in the array.

var animals = [‘cat‘, ‘dog‘, , ‘monkey‘]; // animals is sparse
animals.length // prints 4, but real number of elements is 3

var words = [‘hello‘];
words[6] = ‘welcome‘; //the highest index is 6. words is sparse
words.length //prints 7, based on highest index

When adding or removing elements, length is mutated based on the highest index only. Any array modifications that do not affect the highest index do not modify length, for example when using delete.

var colors = [‘blue‘, ‘red‘, ‘yellow‘, ‘white‘, ‘black‘];
colors.length // prints 5

delete colors[0]; // remove the first element ‘blue‘.
                  // The array becomes sparse

colors.length // still prints 5, because the highest index 4
              // wasn‘t modified

See the example in JS Bin

Length modification

In the previous explanations, the length was read-only. But JavaScript allows to modify this property also. 
Length modification affects the array, depending on the new value and existing highest index. It can remove elements or make the array sparse. 
When the new length number is less or equal than the highest index, any elements whose index is greater or equal than the new size are removed. An useful scenario to remove elements from the end of array.

var numbers = [1, 3, 5, 7, 8];

numbers.length = 3; // modify the array length
numbers // prints [1, 3, 5], elements 7 and 8 are removed

If using a number greater than the highest index (or using a number bigger than current length), the array will become sparse. It‘s rarely useful.

var osTypes = [‘OS X‘, ‘Linux‘, ‘Windows‘];

osTypes.length = 5; // creating a sparse array. Elements at indexes 3 and 4
                    // do not exist

osTypes // prints [‘OS X‘, ‘Linux‘, ‘Windows‘, , , ]

See the examples in JS Bin

It‘s possible to assign a different type than number to length. JavaScript will convert the primitive to a number. If the conversion result is NaN or number less than 0, an error is thrown Uncaught RangeError: Invalid array length.

var numbers = [1, 4, 6, 7];
numbers.length = ‘2‘; // ‘2‘ is converted to number 2
numbers.length = ‘not-number‘; // throws Uncaught RangeError: Invalid array length
numbers.length = -2; // throws Uncaught RangeError: Invalid array length

Code safely

Modifying the array length, removing elements with delete, adding elements with [newIndex] are sources of potential problems by creating sparse arrays. And as result an inconsistent length value. 
JavaScript offers safer alternatives.

To add elements to the end of an array use Array.prototype.push() and to remove the latest pop()
To insert an element to the beginning use unshift() and to remove the first one shift()
For more complex insertions, deletions or replacements, splice() is powerful enough too.

var companies = [‘Apple‘, ‘Dell‘];

companies.push(‘ASUS‘); // Adds an element to the end
companies // prints [‘Apple‘, ‘Dell‘, ‘ASUS‘]

companies.pop();  // prints "ASUS". Removes the last element
companies // prints [‘Apple‘, ‘Dell‘]

companies.shift(); // prints "Apple". Removes the first array element
companies // prints ["Dell"]

companies.splice(1, 0, "Microsoft", "HP"); // Add 2 companies
companies // prints ["Dell", "Microsoft", "HP"]

companies.length // prints 3. The array is dense

See the examples in JS Bin

There are rare situations when the array can be sparse. It‘s not safe to rely on the length to determine the number of elements. Just use a helper function which handles the missing elements:

/**
 * Count the number of elements in a sparse array
 * @param {Array} collection
 * @return {number}
 */
function count(collection) {
  var totalCount = 0;
  for (var index = 0; index < collection.length; index++) {
    if (index in collection) {
      totalCount++;
    }
  }
  return totalCount;
}

in operator determines if the object has a property. It works perfectly to check if an element exists at specific index.

Conclusion

As seen in the article, length is a property with complex behavior. 
Mostly it works without surprises, but it‘s better to take precautions when dealing with sparse arrays and modifying the length
An alternative is avoid at all modifying this property and use the splice() method.

Have a great coding day.

See also 
Array.prototype.length 
Sparse arrays vs dense arrays

时间: 2024-10-20 20:35:33

【转】The magic behind array length property的相关文章

缓存 Array.length 是老生常谈的小优化

问题 缓存 Array.length 是老生常谈的小优化. // 不缓存 for (var i = 0; i < arr.length; i++) { ... } // 缓存 var len = arr.length; for (var i = 0; i < len; i++) { ... } // 或者 for (var i = 0, len = arr.length; i < len; i++) { ... } 但以前写过 Java 的笔者一直对这种破碎的写法感到不适,也对这种写法的

Count and Say (Array Length Encoding) -- LeetCode

The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" or 1211. Given an

Swift String length property

Swift的String竟然没有length属性,好难受,每次要获取String的字符串长度都要借助全局函数countElements. 没办法,只有扩展String结构体,给它添加一个属性了. import Foundation extension String { // readonly computed property var length: Int { return countElements(self) } } let a = "hechengmen" println(a.

数组中array==null和array.length==0的区别

//代码public class Test1 { public static void main(String[] args) { int[] a1 = new int[0]; int[] a2 = null; System.out.println(a1.length);//0 System.out.println(a2.length);//NullPointerException } } //输出结果 "C:\Program Files\Java\jdk1.8.0_191\bin\java&q

9.3 寻找magic index

在A[0..n-1]中,满足条件 A[i]==i的索引. 给定一个有序数组,设法找到其中的magic index. 扩展:考虑有重复元素的情况如何处理. public class Solution { public static int magicIndex(int[] array) { return magicDup(array, 0, array.length - 1); } /** * 非递归,有重复情况可能有问题 */ private static int magic(int[] a,

Javascript Array

Arrays Arrays are zero-indexed, ordered lists of values. They are a handy way to store a set of related items of the same type (such as strings), though in reality, an array can include multiple types of items, including other arrays. To create an ar

iOS runtime探究(三): 从runtime开始理解OC的属性property

你要知道的runtime都在这里 转载请注明出处 http://blog.csdn.net/u014205968/article/details/67639303 本文主要讲解runtime相关知识,从原理到实践,由于包含内容过多分为以下五篇文章详细讲解,可自行选择需要了解的方向: 从runtime开始: 理解面向对象的类到面向过程的结构体 从runtime开始: 深入理解OC消息转发机制 从runtime开始: 理解OC的属性property 从runtime开始: 实践Category添加属

lodash学习笔记之Array方法

今天周末在家无聊学习一下lodash. lodash目前的中文资料很少.而且api好像还被墙了.下面说一下lodash的arrary相关的方法. 1. chunk   英 [t???k]    顾名思义,是对数组进行分块的方法 n. 大块:矮胖的人或物 用法: _.chunk(array,number)  根据number对array进行均等的分块,如果array不能被number平分,则会留下一个余下的块. _.chunk(['a','b','c','d'],-1); //当 size<=1的

JavaScript的进阶之路(三)引用类型之Object类型和Array类型

引用类型 Object类型 function a(num){ if(num>3){ a(--num); } console.log(num); } a(5); //如何创建对象的实例 var obj1= new Object(); console.log(obj1); obj1.name="吴琼"; obj1.age=28; console.log(obj1.name+" "+obj1.age); //对象字面量语法 ,有点封装的感觉 var obj2 = {