ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JavaScript λ°°μ—΄ λ©”μ†Œλ“œμ˜ μ‹œκ°„λ³΅μž‘λ„
    🌐 FrontEnd/JavaScript 2022. 12. 14. 17:36

     

    μ‹œμž‘ν•˜λ©΄μ„œ

    μžλ°”μŠ€ν¬λ¦½νŠΈ λ°°μ—΄ λ©”μ„œλ“œμ— κ΄€ν•œ μ‹œκ°„ λ³΅μž‘λ„λ₯Ό κ³΅λΆ€ν•©λ‹ˆλ‹€.

     

     

    λ°°μ—΄ λ©”μ†Œλ“œμ˜ μ‹œκ°„ λ³΅μž‘λ„

    μ‹œκ°„ λ³΅μž‘λ„λž€?

    μ‹œκ°„ λ³΅μž‘λ„λž€ κ°„λ‹¨ν•˜κ²Œ λ§ν•΄μ„œ μ•Œκ³ λ¦¬μ¦˜μ΄ μ–΄λ–€ 문제λ₯Ό ν•΄κ²°ν•˜λŠ” 데 κ±Έλ¦¬λŠ” μ‹œκ°„μ„ λ§ν•©λ‹ˆλ‹€.

    μ‹œκ°„ λ³΅μž‘λ„ ν‘œμ‹œλŠ” λΉ…μ˜€ ν‘œκΈ°λ²•μ„ μ‚¬μš©ν•˜μ˜€μŠ΅λ‹ˆλ‹€. μ•Œκ³ λ¦¬μ¦˜μ˜ νš¨μœ¨μ„±μ„ ν‘œκΈ°ν•΄μ£ΌλŠ” ν‘œκΈ°λ²•μž…λ‹ˆλ‹€.

     

    λ°°μ—΄ λ©”μ„œλ“œ μ‹œκ°„ λ³΅μž‘λ„

    λ°°μ—΄μ—μ„œ 많이 μ‚¬μš©ν•˜λŠ” λ©”μ„œλ“œλ“€μ˜ μ‹œκ°„ λ³΅μž‘λ„λ₯Ό μ•Œμ•„λ³΄λ„λ‘ ν•˜κ² μŠ΅λ‹ˆλ‹€. 

     

    1. push() - O(1)

    λ°°μ—΄ 끝에 μƒˆλ‘œμš΄ μš”μ†Œλ₯Ό μΆ”κ°€ν•΄μ£ΌλŠ” λ©”μ„œλ“œμž…λ‹ˆλ‹€.

    μΌμ •ν•œ O(1)의 λ³΅μž‘λ„λ₯Ό κ°–μŠ΅λ‹ˆλ‹€. μž…λ ₯값이 μ¦κ°€ν•˜λ”λΌλ„ μ‹œκ°„μ€ λ³€ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

    const names = ['Luis','John','Jose'];
    names.push("Aaron");
    console.log(names); // (4) ["Luis", "John", "Jose", "Aaron"]

     

    2. pop() - O(1)

    λ°°μ—΄ 끝 μš”μ†Œλ₯Ό μ‚­μ œν•΄μ£ΌλŠ” λ©”μ„œλ“œμž…λ‹ˆλ‹€.

    μΌμ •ν•œ O(1)의 λ³΅μž‘λ„λ₯Ό κ°–μŠ΅λ‹ˆλ‹€. μž…λ ₯값이 μ¦κ°€ν•˜λ”λΌλ„ μ‹œκ°„μ€ λ³€ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

    const names = ['Luis','John','Jose','Aaron'];
    console.log(names.pop()); //Aaron
    console.log(names); // (3) ["Luis", "John", "Jose"]

     

    3. shift() - O(n)

    λ°°μ—΄μ˜ 첫번째 μš”μ†Œλ₯Ό μ‚­μ œν•΄μ£ΌλŠ” λ©”μ„œλ“œμž…λ‹ˆλ‹€.

    μ„ ν˜•λ³΅μž‘λ„λΌκ³  λΆ€λ₯΄λŠ” O(n)의 λ³΅μž‘λ„λ₯Ό κ°–μŠ΅λ‹ˆλ‹€. μž…λ ₯κ°’μ˜ 증가도에 따라 μ‹œκ°„λ„ λ™μΌν•œ λΉ„μœ¨λ‘œ μ¦κ°€ν•©λ‹ˆλ‹€.

    const names = ['Luis','John','Jose','Aaron'];
    console.log(names.shift()); // Luis
    console.log(names); // (3) ["John", "Jose", "Aaron"]

     

    4. unshift() - O(n)

    λ°°μ—΄μ˜ μ‹œμž‘ 뢀뢄에 ν•˜λ‚˜ μ΄μƒμ˜ μš”μ†Œλ₯Ό μΆ”κ°€ν•˜λŠ” λ©”μ„œλ“œμž…λ‹ˆλ‹€.

    μ„ ν˜•λ³΅μž‘λ„λΌκ³  λΆ€λ₯΄λŠ” O(n)의 λ³΅μž‘λ„λ₯Ό κ°–μŠ΅λ‹ˆλ‹€. μž…λ ₯κ°’μ˜ 증가도에 따라 μ‹œκ°„λ„ λ™μΌν•œ λΉ„μœ¨λ‘œ μ¦κ°€ν•©λ‹ˆλ‹€.

    const names = ['Luis','John','Jose'];
    console.log(names.unshift("Aaron")); // 4
    console.log(names); // (4) ["Aaron", "Luis", "John", "Jose"]

     

    5. splice() - O(n)

    νŠΉμ • μš”μ†Œλ₯Ό μ œκ±°ν•˜κ±°λ‚˜ μΆ”κ°€ λ˜λŠ” κ΅μ²΄ν•˜λŠ” λ©”μ„œλ“œμž…λ‹ˆλ‹€.

    μ„ ν˜•λ³΅μž‘λ„λΌκ³  λΆ€λ₯΄λŠ” O(n)의 λ³΅μž‘λ„λ₯Ό κ°–μŠ΅λ‹ˆλ‹€. μž…λ ₯κ°’μ˜ 증가도에 따라 μ‹œκ°„λ„ λ™μΌν•œ λΉ„μœ¨λ‘œ μ¦κ°€ν•©λ‹ˆλ‹€.

    const months = ["Jan", "March", "April", "June"];
    months.splice(1, 0, "Feb");
    // inserts at index 1
    console.log(months);
    // expected output: Array ["Jan", "Feb", "March", "April", "June"]
    
    months.splice(4, 1, "May");
    // replaces 1 element at index 4
    console.log(months);
    // expected output: Array ["Jan", "Feb", "March", "April", "May"]

     

    6. sort() - O(n log(n))

    λ°°μ—΄μš”μ†Œλ₯Ό μ •λ ¬ν•΄μ£ΌλŠ” λ©”μ„œλ“œ μž…λ‹ˆλ‹€.

    데이터가 λ§Žμ•„μ§€μˆ˜λ‘ μ²˜λ¦¬μ‹œκ°„μ΄ 둜그(log) 배만큼 더 λŠ˜μ–΄λ‚˜λŠ” O(n log(n))의 λ³΅μž‘λ„λ₯Ό κ°–μŠ΅λ‹ˆλ‹€. 데이터가 10배라면, μ²˜λ¦¬μ‹œκ°„μ€ μ•½ 20λ°°λ₯Ό κ°–λŠ” λ³΅μž‘λ„μž…λ‹ˆλ‹€. 

    const months = ['March', 'Jan', 'Feb', 'Dec'];
    months.sort();
    console.log(months);
    // expected output: Array ["Dec", "Feb", "Jan", "March"]
    
    const array1 = [1, 30, 4, 21, 100000];
    array1.sort();
    console.log(array1);
    // expected output: Array [1, 100000, 21, 30, 4]

     

    7. concat() - O(n)

    2개 μ΄μƒμ˜ 배열을 ν•©μ³μ£ΌλŠ” λ©”μ„œλ“œμž…λ‹ˆλ‹€.

    μ„ ν˜•λ³΅μž‘λ„λΌκ³  λΆ€λ₯΄λŠ” O(n)의 λ³΅μž‘λ„λ₯Ό κ°–μŠ΅λ‹ˆλ‹€. μž…λ ₯κ°’μ˜ 증가도에 따라 μ‹œκ°„λ„ λ™μΌν•œ λΉ„μœ¨λ‘œ μ¦κ°€ν•©λ‹ˆλ‹€.

    const names1 = ["Luis","Jose"];
    const names2 = ["John","Aaron"];
    const newArray = names1.concat(names2,["Michelle"]);
    console.log(newArray); // (5) ["Luis", "Jose", "John", "Aaron", "Michelle"]

     

    8. slice() - O(n)

    μ–΄λ–€ λ°°μ—΄μ˜ μ‹œμž‘λΆ€ν„° λκΉŒμ§€(끝은 ν¬ν•¨ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€) μž˜λΌμ„œ 볡사본을 μƒˆλ‘œμš΄ λ°°μ—΄ 객체둜 λ°˜ν™˜ν•˜λŠ” λ©”μ„œλ“œμž…λ‹ˆλ‹€.

    μ„ ν˜•λ³΅μž‘λ„λΌκ³  λΆ€λ₯΄λŠ” O(n)의 λ³΅μž‘λ„λ₯Ό κ°–μŠ΅λ‹ˆλ‹€. μž…λ ₯κ°’μ˜ 증가도에 따라 μ‹œκ°„λ„ λ™μΌν•œ λΉ„μœ¨λ‘œ μ¦κ°€ν•©λ‹ˆλ‹€.

    const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
    
    console.log(animals.slice(2));
    // expected output: Array ["camel", "duck", "elephant"]
    
    console.log(animals.slice(2, 4));
    // expected output: Array ["camel", "duck"]
    
    console.log(animals.slice(1, 5));
    // expected output: Array ["bison", "camel", "duck", "elephant"]
    
    console.log(animals.slice(-2));
    // expected output: Array ["duck", "elephant"]
    
    console.log(animals.slice(2, -1));
    // expected output: Array ["camel", "duck"]
    
    console.log(animals.slice());
    // expected output: Array ["ant", "bison", "camel", "duck", "elephant"]

     

    9. indexOf() - O(n)

    배열에 μ‘΄μž¬ν•˜λŠ” μš”μ†Œλ₯Ό μ°Ύμ•„μ„œ ν•΄λ‹Ή 인덱슀λ₯Ό λ°˜ν™˜ν•΄μ£ΌλŠ” λ©”μ„œλ“œμž…λ‹ˆλ‹€. μ—¬λŸ¬κ°œμΌ 경우 κ°€μž₯ 첫번째만, 없을 κ²½μš°μ—” -1을 λ°˜ν™˜ν•©λ‹ˆλ‹€.

    μ„ ν˜•λ³΅μž‘λ„λΌκ³  λΆ€λ₯΄λŠ” O(n)의 λ³΅μž‘λ„λ₯Ό κ°–μŠ΅λ‹ˆλ‹€. μž…λ ₯κ°’μ˜ 증가도에 따라 μ‹œκ°„λ„ λ™μΌν•œ λΉ„μœ¨λ‘œ μ¦κ°€ν•©λ‹ˆλ‹€.

    const names = ['Luis','Jose','John','Aaron'];
    console.log(names.indexOf("John")); // 2
    console.log(names.indexOf("Michelle")); // -1

     

    10. forEach() - O(n)

    λ°°μ—΄μ˜ 각 μš”μ†Œμ— λŒ€ν•΄ ν•¨μˆ˜λ₯Ό μ‹œν–‰ν•˜λŠ” λ©”μ„œλ“œ μž…λ‹ˆλ‹€.

    μ„ ν˜•λ³΅μž‘λ„λΌκ³  λΆ€λ₯΄λŠ” O(n)의 λ³΅μž‘λ„λ₯Ό κ°–μŠ΅λ‹ˆλ‹€. μž…λ ₯κ°’μ˜ 증가도에 따라 μ‹œκ°„λ„ λ™μΌν•œ λΉ„μœ¨λ‘œ μ¦κ°€ν•©λ‹ˆλ‹€.

    const array1 = ['a', 'b', 'c'];
    
    array1.forEach(element => console.log(element));
    
    // expected output: "a"
    // expected output: "b"
    // expected output: "c"

     

    11. map() - O(n)

    λ°°μ—΄μ˜ 각 μš”μ†Œμ— λŒ€ν•΄ ν•¨μˆ˜λ₯Ό μ‹œν–‰ν•˜λŠ” λ©”μ„œλ“œ μž…λ‹ˆλ‹€.

    μ„ ν˜•λ³΅μž‘λ„λΌκ³  λΆ€λ₯΄λŠ” O(n)의 λ³΅μž‘λ„λ₯Ό κ°–μŠ΅λ‹ˆλ‹€. μž…λ ₯κ°’μ˜ 증가도에 따라 μ‹œκ°„λ„ λ™μΌν•œ λΉ„μœ¨λ‘œ μ¦κ°€ν•©λ‹ˆλ‹€.

    const array1 = [1, 4, 9, 16];
    
    // pass a function to map
    const map1 = array1.map(x => x * 2);
    
    console.log(map1);
    // expected output: Array [2, 8, 18, 32]

     

    12. filter() - O(n)

    주어진 ν•„ν„° 쑰건을 ν†΅κ³Όν•˜λŠ” λͺ¨λ“  μš”μ†Œλ₯Ό λͺ¨μ•„ μƒˆλ‘œμš΄ λ°°μ—΄λ‘œ λ°˜ν™˜ν•˜λŠ” λ©”μ„œλ“œ μž…λ‹ˆλ‹€.

    μ„ ν˜•λ³΅μž‘λ„λΌκ³  λΆ€λ₯΄λŠ” O(n)의 λ³΅μž‘λ„λ₯Ό κ°–μŠ΅λ‹ˆλ‹€. μž…λ ₯κ°’μ˜ 증가도에 따라 μ‹œκ°„λ„ λ™μΌν•œ λΉ„μœ¨λ‘œ μ¦κ°€ν•©λ‹ˆλ‹€.

    const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
    
    const result = words.filter(word => word.length > 6);
    
    console.log(result);
    // expected output: Array ["exuberant", "destruction", "present"]

     

    13. reduce() - O(n)

    λ°°μ—΄μ˜ 각 μš”μ†Œμ— λŒ€ν•΄ 주어진 λ¦¬λ“€μ„œ (reducer) ν•¨μˆ˜λ₯Ό μ‹€ν–‰ν•˜κ³ , ν•˜λ‚˜μ˜ 결과값을 λ°˜ν™˜ν•˜λŠ” λ©”μ„œλ“œμž…λ‹ˆλ‹€.

    μ„ ν˜•λ³΅μž‘λ„λΌκ³  λΆ€λ₯΄λŠ” O(n)의 λ³΅μž‘λ„λ₯Ό κ°–μŠ΅λ‹ˆλ‹€. μž…λ ₯κ°’μ˜ 증가도에 따라 μ‹œκ°„λ„ λ™μΌν•œ λΉ„μœ¨λ‘œ μ¦κ°€ν•©λ‹ˆλ‹€.

    const array1 = [1, 2, 3, 4];
    
    // 0 + 1 + 2 + 3 + 4
    const initialValue = 0;
    const sumWithInitial = array1.reduce(
      (accumulator, currentValue) => accumulator + currentValue,
      initialValue
    );
    
    console.log(sumWithInitial);
    // expected output: 10

     

     

    λ§ˆλ¬΄λ¦¬ν•˜λ©°

    μ΄λ ‡κ²Œ 각 JavaScript λ°°μ—΄ λ©”μ„œλ“œμ˜ μ‹œκ°„ λ³΅μž‘λ„μ— λŒ€ν•΄μ„œ μ•Œμ•„λ³΄μ•˜μŠ΅λ‹ˆλ‹€.

    차후에 λ°°μ—΄ λ©”μ„œλ“œλ₯Ό μ΄μš©ν•œ κΈ°λŠ₯을 λ§Œλ“€ λ•Œ μ‹œκ°„ λ³΅μž‘λ„λ₯Ό μƒκ°ν•˜λ©΄μ„œ κ΅¬ν˜„ν•˜λ©΄ 쒋을 λ“― ν•©λ‹ˆλ‹€.

     

    Reference

    https://dev.to/lukocastillo/time-complexity-big-0-for-javascript-array-methods-and-examples-mlg

     

    Time complexity Big 0 for Javascript Array methods and examples.

    Hello everyone, some weeks ago I started to study some computer science algorithms using JavaScript a...

    dev.to

     

    λŒ“κΈ€