subtlety about integer key of array
Version
3.0.11
Reproduction link
https://codesandbox.io/s/modest-chatelet-rx10u?file=/src/main.js
Steps to reproduce
open codesanbox console
What is expected?
effect should be called 1 times
What is actually happening?
effect is called 2 times
The valid array length is from 0
to Math.pow(2, 32) - 1
, AKA 4294967295
. If a array integer key is over valid array length, it will be treated as a object key not a array index.
using valid integer key set array will change the length of array.
using non-valid integer key set array does not change the length of array.
array.length
will never be bigger than4294967295
@iheyunfei
It looks like a clarification of the fix required.
Check latest chrome
let arr = [];
const MAX_VALID_ARRAY_LENGTH = 4294967295; // Math.pow(2, 32) - 1
// correct array length
arr = [];
arr[MAX_VALID_ARRAY_LENGTH - 1] = true;
console.log(arr.length);
if (arr.length === MAX_VALID_ARRAY_LENGTH) {
console.log('correct MAX_VALID_ARRAY_LENGTH');
}
// incorrect array length
arr = [];
arr[MAX_VALID_ARRAY_LENGTH] = true;
console.log(arr.length);
if (arr.length === 0) {
console.log('incorrect MAX_VALID_ARRAY_LENGTH');
}
// incorrect array length arr = []; arr[MAX_VALID_ARRAY_LENGTH] = true; console.log(arr.length); if (arr.length === 0) { console.log('incorrect MAX_VALID_ARRAY_LENGTH'); }
This looks like a correct array length to me. The arr.length should be
MAX_VALID_ARRAY_LENGTH
not 0. The valid array length is from 0 to Math.pow(2, 32) - 1, AKA4294967295
. If a array key is over valid array length, it will be treated as a object key not a array index.