/
πŸ“™

JS Optimize code style

JavaScript
Table of contents
  • Reference

배열을 μˆœνšŒν•˜λ©΄μ„œ accumulator와 valueλ₯Ό λ”ν•΄μ„œ sum을 λ§Œλ“€κ³ , λ§ˆμ§€λ§‰μ— λ°°μ—΄μ˜ 크기둜 λ‚˜λˆ„λŠ” 둜직

js
const data = [1, 2, 3, 4, 5, 6, 1];
const reducer = (accumulator, value, index, array) => {
const sumOfAccAndVal = accumulator + value;
if (index === array.length - 1) {
return (sumOfAccAndVal) / array.length;
}
return sumOfAccAndVal;
};
const getMean = data.reduce(reducer, 0);
console.log(getMean); // 3.142857142857143

μ΄ˆκΈ°κ°’μ„ 0으둜 μ„€μ •ν•˜μ§€ μ•Šμ•„λ„ 첫 번째 인자인 data[0]κ°€ accumulator둜 λ„˜μ–΄κ°„λ‹€.

ν•˜μ§€λ§Œ μ΄ˆκΈ°κ°’μ„ μ„€μ •ν•˜λŠ” 것이 더 μ•ˆμ „ν•˜λ‹€.

js
const data = [1, 2, 3, 4, 5, 6, 1];
const reducer = (accumulator, value, index, array) => {
const arrLength = array.length; // μƒμœ„ μŠ€μ½”ν”„ 탐색을 쀄일 수 μžˆλ‹€.
const sumOfAccAndVal = accumulator + value;
if (index === arrLength - 1) {
return (sumOfAccAndVal) / arrLength;
}
return sumOfAccAndVal;
};
const getMean = data.reduce(reducer, 0);
console.log(getMean); // 3.142857142857143

const arrLength = array.length와 같이 λ°°μ—΄μ˜ 길이λ₯Ό λ³€μˆ˜λ‘œ μ„ μ–Έν•΄μ„œ μž¬μ‚¬μš©ν•˜λ©΄ μƒμœ„ μŠ€μ½”ν”„λ‘œ μ΄λ™ν•΄μ„œ νƒμƒ‰ν•˜κ³  μ°Έμ‘°λ₯Ό ν•œλ²ˆλ§Œ μˆ˜ν–‰ν•˜λ©΄ 되기 λ•Œλ¬Έμ— μ„±λŠ₯을 높일 수 μžˆλ‹€.

ν•˜μ§€λ§Œ μœ„μ™€ 같은 κ²½μš°μ—μ„œλŠ” 였히렀 λΉ„νš¨μœ¨μ μ΄λ‹€.
if 문이 λ°°μ—΄μ˜ λ§ˆμ§€λ§‰ μš”μ†Œμ— 도달 ν–ˆμ„λ•Œ λ™μž‘ν•˜λŠ” 쑰건이기 λ•Œλ¬Έμ— μš”μ†Œμ˜ λ§ˆμ§€λ§‰μ—λ§Œ 두 번의 μƒμœ„ μŠ€μ½”ν”„ 탐색, μ°Έμ‘°κ°€ μΌμ–΄λ‚˜κΈ° λ•Œλ¬Έμ΄λ‹€.

μƒμœ„ μŠ€μ½”ν”„μ—μ„œ μ°Έμ‘°ν•œ 값을 μƒˆλ‘œμš΄ λ³€μˆ˜μ— ν• λ‹Ήν•˜κ³  λ³€μˆ˜λ₯Ό λ‹€μ‹œ μ°Έμ‘°ν•΄μ•Ό ν•˜κΈ° λ•Œλ¬Έμ΄λ‹€.

if (index === array.length - 1) 같이 μ‚¬μš©ν• λ•ŒλŠ” λ³€μˆ˜ μ„ μ–Έκ³Ό 할당이 과정이 쀄어든닀.

μ•„μ£Ό λ―Έμ„Έν•œ μ°¨μ΄μ§€λ§Œ λ°μ΄ν„°μ˜ 양이 λ§Žλ‹€λ©΄ 큰 차이가 λ°œμƒν•  수 μžˆλ‹€.

가독성을 신경쓰지 μ•Šκ³  μ•„μ£Ό κ²½λ―Έν•œ μ°¨μ΄μ§€λ§Œ μ„±λŠ₯만 μƒκ°ν•œλ‹€λ©΄ μ΄λ ‡κ²Œ λ§Œλ“€μˆ˜ μžˆλ‹€.

js
const data = [1, 2, 3, 4, 5, 6, 1];
const reducer = (accumulator, value, index, array) => {
if (index === array.length - 1) (accumulator + value) / array.length;
return accumulator + value;
};
const getMean = data.reduce(reducer, 0);
console.log(getMean); // 3.142857142857143

if 문이 값을 λ°˜ν™˜λ§Œ ν•œλ‹€λ©΄ return을 μƒλž΅ν•  수 μžˆλ‹€.

근데 μƒλž΅ν•˜λ©΄ μžλ°”μŠ€ν¬λ¦½νŠΈ 엔진이 λ¬Έλ§₯을 νŒŒμ•…ν•  λ•Œ μžλ™μœΌλ‘œ μ‚½μž…μ„ ν•΄μ£ΌλŠ”λ°

κ·Έ κ³Όμ •μ—μ„œ μ„±λŠ₯ν•˜λ½μ΄ λ°œμƒν• κΉŒ?

μ•„μ£Όμ•„μ£Ό λ―Έμ„Έν•œ μ°¨μ΄μ§€λ§Œ return을 μƒλž΅ν–ˆμ„λ•Œκ°€ 더 λΉ¨λžλ‹€. μ™œμ§€...?

μžλ™μœΌλ‘œ μ‚½μž…ν•˜λŠ” 것이 μ•„λ‹ˆλΌ μƒλž΅ν•΄λ„ 같은 λ¬Έλ§₯으둜 μ΄ν•΄ν•˜κΈ° λ•Œλ¬ΈμΈκ°€?

ν† ν¬λ‚˜μ΄μ§•μ΄λΌλ˜κ°€ ν•˜λŠ” 과정이 μ€„μ–΄μ„œ 인가? 흠..

평탄화(flatten)

js
const data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
const flatArrayReducer = (accumulator, value, index, array) => {
return accumulator.concat(value);
};
const flattenedData = data.reduce(flatArrayReducer, []);
// [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
js
const data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
const flatArrayReducer = (accumulator, value, index, array) => {
return [...accumulator, ...value];
};
const flattenedData = data.reduce(flatArrayReducer, []);
console.log(flattenedData);
js
const data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
const flatArrayReducer = (accumulator, value, index, array) => [...accumulator, ...value];
const flattenedData = data.reduce(flatArrayReducer, []);
console.log(flattenedData);

.concat()을 μ‚¬μš©ν•œ 것 λ³΄λ‹€λŠ” μŠ€ν”„λ ˆλ“œ μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•œ 것이 더 λΉ¨λžλ‹€.

ν™”μ‚΄ν‘œ ν•¨μˆ˜μ—μ„œ μ€‘κ΄„ν˜Έλ₯Ό μƒλž΅ν•˜μ—¬ λ¦¬ν„΄ν•œ 것 보닀 μ€‘κ΄„ν˜Έλ₯Ό μ‚¬μš©ν•˜κ³  λͺ…μ‹œμ μœΌλ‘œ return을 ν•œ 것이 μ•„μ£Όμ•„μ£Όμ•„μ£Όμ•„μ£Ό κ·Όμ†Œν•œ μ°¨μ΄μ§€λ§Œ 더 λΉ¨λžλ‹€.

μ™œ...?

var ν‚€μ›Œλ“œκ°€ μŠ€μ½”ν”„λ‘œ μΈμ •ν•˜λŠ” λ²”μœ„μ™€ 연관이 μžˆμ„ 것 κ°™λ‹€.

ν•¨μˆ˜ λ²”μœ„ μŠ€μ½”ν”„ 같은 κ²½μš°λŠ” μ€‘κ΄„ν˜Έλ₯Ό μƒλž΅ν•œλ‹€λ©΄ 엔진이 λ¬Έλ§₯을 νŒŒμ•…ν•˜κ³  μŠ€μ½”ν”„λ₯Ό λ§Œλ“€μ–΄μ€˜μ•Ό ν•˜μ§€λ§Œ

if λ¬Έ 같은 κ²½μš°λŠ” const, letν‚€μ›Œλ“œκ°€ μžˆλŠ”κ²Œ μ•„λ‹ˆλΌλ©΄ μŠ€μ½”ν”„λ₯Ό μƒλž΅ν•˜κΈ° λ•Œλ¬ΈμΌκΉŒ?

Reference

[μžλ°”μŠ€ν¬λ¦½νŠΈ] μ„±λŠ₯을 λ†’μ΄λŠ” μ½”λ“œ μŠ€νƒ€μΌ

logo
Things I've Learned