-
Notifications
You must be signed in to change notification settings - Fork 0
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
JavaScript에서 shallow copy 피하기 #3
Comments
// Array(...)로 생성 후 for문 사용 - 가장 빠름
const arr = Array(...);
for (let i = 0; i < arr.length; i++) {
arr[i] = [];
}
// 배열 리터럴로 생성 후 Array.push - 2배 느림
const arr = [];
for (let i = 0; i < arr.length; i++) {
arr.push([]);
}
// destructuring 사용 - 5배 느림
[...Array(...)].map(() => []);
// forEach - 6배 느림
const arr = Array(...);
arr.forEach((value, index) => {
arr[i] = [];
});
// map 사용 - 11배 느림
Array(...).fill().map(() => []);
// Array.from()을 사용 - 30배 느림
Array.from(Array(...), () => [])); reference: https://stackoverflow.com/a/43461022 |
Array() 생성자를 선언할 때 new 연사자를 함께 사용하는 것이 맞는걸까? 공식문서를 보면 항상 new 연산자와 함께 선언하고 있어서 궁금해서 찾아봤다. ECMA-262 문서를 보면 아래와 같이 설명하고 있다. 결론적으론 new 연산자 없이 선언해도 상관없다.
reference: https://scottwhittaker.net/posts/javascript-array-constructor-called-as-a-function/ |
Array
The text was updated successfully, but these errors were encountered: