【转】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: 0, 1 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: 0, 2 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
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
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', , , ]
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
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
【转】The magic behind array length property的更多相关文章
- Array.length vs Array.prototype.length
I found that both the Array Object and Array.prototype have the length property. I am confused on us ...
- [Bug]The maximum array length quota (16384) has been exceeded while reading XML data.
写在前面 在项目中,有客户反应无法正常加载组织结构树,弄了一个测试的程序,在日志中查看到如下信息: Error in deserializing body of reply message for o ...
- 缓存 Array.length 是老生常谈的小优化
问题 缓存 Array.length 是老生常谈的小优化. // 不缓存 for (var i = 0; i < arr.length; i++) { ... } // 缓存 var len = ...
- check the element in the array occurs more than half of the array length
Learn this from stackflow. public class test { public static void main(String[] args) throws IOExcep ...
- 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, ...
- Math.floor(Math.random() * array.length),splice
1.Math.floor(Math.random() * array.length) 返回长度内的索引 eg: changeLimit () { function getArrayItems(arr, ...
- javascript change array length methods
javascript change array length methods Array 改变数组长度的方法 push, pop shift, unshift, splice, fill, 不改变数组 ...
- Swift String length property
Swift的String居然没有length属性,好难受,每次要获取String的字符串长度都要借助全局函数countElements. 没办法.仅仅有扩展String结构体,给它加入一个属性了. i ...
- how to increase an regular array length in java?
Arrays in Java are of fixed size that is specified when they are declared. To increase the size of t ...
随机推荐
- java 图形界面 mvc模式控制
使用模型-视图-控件结构来开发GUI程序. 下面的程序演示了MVC模式开发的java程序. 其中CircleModel为模型,包含了圆的半径,是否填充,等属性. CircleView为视图,显示这个圆 ...
- 基于hadoop的图书推荐
根据在炼数成金上的学习,将部分代码总结一下在需要的时候可以多加温习.首先根据原理作简要分析.一般推荐系统使用的协同过滤推荐模型:分别是基于ItemCF的推荐模型或者是基于UserCF的推荐模型:首先分 ...
- 【2】docker 与UFW
ufw是一个主机端的iptables类防火墙配置工具. 1.查看ufw的状态:ufw status 2.如果使用UFW,需要对其进行修改才能让DOCKER工作.docker使用一个网桥来管理容器中的网 ...
- Hibernate与Sleep的区别
转自:http://blog.sina.com.cn/s/blog_4b6e98810100n37k.html 休眠(Hibernate),将系统切换到该模式后,系统会自动将内存中的数据全部转存到硬盘 ...
- linux上安装配置samba服务器
linux上安装配置samba服务器 在linux上安装配置samba服务器 在这给大家介绍一个不错的家伙,samba服务.如果您正在犯愁,如何在Windows和Linux之间实现资源共享,就请看看这 ...
- leetcode136 利用异或运算找不同的元素
Given an array of integers, every element appears twice except for one. Find that single one. Note: ...
- java常量和变量的定义规则,变长参数的使用
首先是定义的一般规则,类名首字母全部大写,常量全部大写用下划线分隔,变量用驼峰形式.注意使用long赋值用L时不能写小写的L要写大写的,不然会和数字“1”傻傻分不清. 下面是举例: public cl ...
- R语言笔记1--向量、数组、矩阵、数据框、列表
注释:R语言是区分大小写的 1.向量 R语言中可以将各种向量赋值为一个变量,这种赋值操作符就是等号“=”,也可以使用“<-”. 1)产生向量 (1)函数c() 例如:x1=c(2,4,6,8,0 ...
- 【转】PHP android ios相互兼容的AES加密算法
APP项目用户密码传输一直没有用HTTPS,考虑到用户的隐私暂时先用AES对密码加密,以后也可以用于手机端与服务端加密交互. PHP的免费版phpAES项目,手机端解码各种不对. 好不容易找了PHP ...
- Spring.NET 中的 ADO.NET 数据访问的示例
Spring.NET 1.3.1 中提供了一个使用 AdoTemplate 的完整示例,包括使用泛型和使用非泛型技术进行数据访问,这个示例位于下载的压缩包中\Spring.NET-1.3.1\Spri ...