Skip to main content

Tips

About 2 minNode.jsTipsjsnodenodejstipssnippets

Tips 관련


One-liner

Capitalize Text
const capitalize = (str) => `${str.charAt(0).toUpperCase()}${str.slice(1)}`;

const name = "robert";
capitalize(name) 
// Result: "Robert"

Number

Truncate an Array
let array = [0, 1, 2, 3, 4, 5];
array.length = 3;
// Result: [0, 1, 2]

Performance

console.time()

// Our test function runs a big for-loop
function test() {
    let number = 0;
    for (let i=0; i<999999; i++) 
      number += 5;
}


console.time("Test function");     // Start measuring time
test();                            // Call our test function
console.timeEnd("Test function");  // stop measuring time

/*
 default: 1.205810546875ms
 (value may vary depnding on browser, etc.)
 */

performance.now()

let start = performance.now();     // Timestamp before execution
test();                            // Call our test function
let end  = performance.now();      // Timestamp after execution
// Calculate the time taken (in ms)
let ms = end - start;
console.log(ms);
/*
 default: 1.205810546875ms
 */

이찬희 (MarkiiimarK)
Never Stop Learning.