dense-to-sparse.js (1463B)
1 // |jit-test| allow-oom 2 // Appending elements to a dense array should make the array sparse when the 3 // length exceeds the limit. 4 5 function test() { 6 const MAX_DENSE_ELEMENTS_ALLOCATION = (1 << 28) - 1; 7 const VALUES_PER_HEADER = 2; 8 const MAX_DENSE_ELEMENTS_COUNT = MAX_DENSE_ELEMENTS_ALLOCATION - VALUES_PER_HEADER; 9 const SPARSE_DENSITY_RATIO = 8; 10 const MIN_DENSE = MAX_DENSE_ELEMENTS_COUNT / SPARSE_DENSITY_RATIO; 11 const MARGIN = 16; 12 13 let a = []; 14 // Fill the beginning of array to make it keep dense until length exceeds 15 // MAX_DENSE_ELEMENTS_COUNT. 16 for (let i = 0; i < MIN_DENSE; i++) 17 a[i] = i; 18 19 // Skip from MIN_DENSE to MAX_DENSE_ELEMENTS_COUNT - MARGIN, to reduce the 20 // time taken by test. 21 22 // Fill the ending of array to make it sparse at MAX_DENSE_ELEMENTS_COUNT. 23 for (let i = MAX_DENSE_ELEMENTS_COUNT - MARGIN; i < MAX_DENSE_ELEMENTS_COUNT + MARGIN; i++) 24 a[i] = i; 25 26 // Make sure the last element is defined. 27 assertEq(a.length, MAX_DENSE_ELEMENTS_COUNT + MARGIN); 28 assertEq(a[a.length - 1], MAX_DENSE_ELEMENTS_COUNT + MARGIN - 1); 29 30 // Make sure elements around MAX_DENSE_ELEMENTS_COUNT are also defined. 31 assertEq(a[MAX_DENSE_ELEMENTS_COUNT - 1], MAX_DENSE_ELEMENTS_COUNT - 1); 32 assertEq(a[MAX_DENSE_ELEMENTS_COUNT], MAX_DENSE_ELEMENTS_COUNT); 33 assertEq(a[MAX_DENSE_ELEMENTS_COUNT + 1], MAX_DENSE_ELEMENTS_COUNT + 1); 34 } 35 36 // Takes too long time on debug build. 37 if (!getBuildConfiguration("debug")) { 38 test(); 39 }