-
카테고리
-
세부 분야
프로그래밍 언어
-
해결 여부
미해결
성능 차이가 정말 가시적일까요?
22.04.27 19:18 작성 조회수 200
0
강의에서는 2개의 케이스를 작성해서 일반 함수 / 제네레이터 함수 에 대해서 언급을 해주시면서 화면상에는 성능이 약 1.8 배 정도 난 걸로 보았습니다.
정말 그렇게 큰 차이가 나는가 라는 생각을 해서 다음과 같은 케이스를 검증해보았습니다.
# 케이스
1. new Array() > push()
2. new Array(length); > array[index] = i
3. Array.from({ length: l }, ( _, i ) => i);
4. 강의에 나온 제네레이터 함수
# 결과
빠르네요!!
## 10_000 기준 (8.87% 빠름)
## 100_000 기준 (0.2396% 느림)
## 1_000_000 기준 (8.8299..% 빠름)
## 10_000_000 기준 (11.42% 빠름)
## 코드
const range1 = l => {
let i = -1;
let res = [];
while (++i < l) {
res.push(i);
}
return res;
};
const range2 = l => {
let i = -1;
let res = new Array(l);
while (++i < l) {
res[i - 1] = i;
}
return res;
};
const range3 = l => Array.from({ length: l }, (_, i) => ++i);
const L = {};
L.range = function *(l) {
let i = -1;
while (++i < l) {
yield i;
}
}
function test(name, time, f) {
console.time(name);
while(time--) f();
console.timeEnd(name);
}
test('push', 10, () => customReduce(add, range1(1_000_000)));
test('index', 10, () => customReduce(add, range2(1_000_000)));
test('Array.from', 10, () => customReduce(add, range3(1_000_000)));
test('Generator', 10, () => customReduce(add, L.range(1_000_000)));
답변을 작성해보세요.
1
unchaptered
질문자2022.04.27
관련해서 강사님께서 이미 답변하신 내용이 존재해서 링크업만 해둡니다!
순회할 값이 적은 경우에는 왜 먼저 실행한 게 오래 걸리나요? - 인프런 | 질문 & 답변 (inflearn.com)
0
답변 2