Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,563 questions

56,778 answers

573 users

How to measure the time of a reverse string in JavaScript

4 Answers

0 votes
/**
 * Reverse a string using common built‑in methods.
 * The process:
 * - Convert the string into an array of characters.
 * - Reverse the array.
 * - Join it back into a string.
 */
function reverseString(str) {
  return str.split('').reverse().join('');
}

/**
 * Measure how long a function takes to run.
 * This uses console timers, which are convenient for quick checks.
 *
 * @param {Function} fn  The function to execute.
 * @param {...any} args  Arguments passed to the function.
 * @returns {any}        The function's return value.
 */
function time(fn, ...args) {
  console.time("timer");        // Start a named timer
  const result = fn(...args);   // Run the function and capture its output
  console.timeEnd("timer");     // Stop the timer and print the duration
  
  return result;                // Return the function's result
}

// Run the timing utility with a sample input
time(reverseString, "A String");


/*
run:

timer: 0.022ms

*/

 



answered Aug 21 by avibootz
edited Aug 21 by avibootz
0 votes
function reverseString(str) {
  let reversed = "";
  const len = str.length;
  
  for(let i = len - 1; i >= 0; i--) {
    reversed = `${reversed}${str[i]}`;
  }

  return reversed;
}


function time(fn, ...args) {
  console.time("timer");
  const result = fn(...args);
  console.timeEnd("timer");

  return result;
}

time(reverseString, "A String");


/*
run:

timer: 0.031ms

*/

 



answered Aug 21 by avibootz
edited Aug 21 by avibootz
0 votes
function reverseString (str) {
  let reversed = '';

  for (const character of str) {
    reversed = character + reversed
  }

  return reversed
}


function time(fn, ...args) {
  console.time("timer");
  const result = fn(...args);
  console.timeEnd("timer");

  return result;
}

time(reverseString, "A String");



/*
run:

timer: 0.035ms

*/

 



answered Aug 21 by avibootz
0 votes
function reverseString (str) {
  return [...str].reverse().join('')
}
 
function time(fn, ...args) {
  console.time("timer");
  const result = fn(...args);
  console.timeEnd("timer");
 
  return result;
}
 
time(reverseString, "A String");
 
 
/*
run:
 
timer: 0.019ms
 
*/

 



answered Aug 21 by avibootz
...