New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
clone new Array(number); #3364
Comments
Welcome to the wonderful world of javascript! Kidding aside. The behavior of console.log(new Array(5).fill(undefined)) // [undefined, undefined, undefined, undefined, undefined] , length: 5 The way that Ramda clones arrays is by starting with an empty array and cloning each This means that you put in an Array of length 5, and get out an Array of length 0. However, both the input and output array are still technically empty. In general, we get a lot of variety on if const data = new Array(5);
R.equals([], data); // true
lodash.isEqual([], data)); // false
data[0]; // undefined for `0` or any index
[...data]; // [undefined, undefined, undefined, undefined, undefined] , length: 5
Array.from(data); // [undefined, undefined, undefined, undefined, undefined] , length: 5
Obect.keys(data); // []
data.forEach(v => console.log(v)); // noop
for (let i; i <= data.length; i++) console.log(data[i]); // `undefined` x5
for (const key in data) console.log(key); // noop
for (const value of data) console.log(value); // // `undefined` x5
console.log(structuredClone(new Array(5))); // [] , length: 5 The question is: do we respect the value of the My vote would be to do what This would simply mean, instead of starting the clone with an empty array |
@Harris-Miller wow thx, didn't expect such a comment with good educational materials. I wonder does javascript still handle it internally and correctly like other langs but with that quirky behavior, or it's just the same as dynamic array |
According to this stack overflow post, it does matter |
@CrossEye @kedashoe @adispring This is a worth-file micro-optimization for any point within ramda where we are creating a new array where the length is known ahead of time, Looks like // _clone.js
// is
case 'Array': return copy([]);
// could be
case 'Array': return copy(Array(value.length)); I'm sure there are other locations as well |
I realized that ramda will remove all value from the new Array initialization with predefine size, is this intentional?
as oppose to Lodash
The text was updated successfully, but these errors were encountered: