Subscribe on changes!

subtlety about integer key of array

avatar
May 21st 2021

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 than 4294967295

avatar
May 21st 2021

@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');
}
avatar
May 22nd 2021
// 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, AKA 4294967295. If a array key is over valid array length, it will be treated as a object key not a array index.

avatar
May 23rd 2021

move to #3816 .