How to measure the time of a reverse string in TypeScript

4 Answers

0 votes
/**
 * Reverse a string using built‑in methods.
 * Types are explicit for clarity.
 */
function reverseString(str: string): string {
  return str.split("").reverse().join("");
}

/**
 * Measure the time it takes to run a function.
 * Uses console timers for quick debugging.
 */
function timeFunction(label: string, fn: (arg: string) => string, arg: string): string {
  console.time(label);          // start timing
  const result: string = fn(arg);
  console.timeEnd(label);       // end timing and print duration

  return result;
}

// Run the measurement
const output: string = timeFunction("function test", reverseString, "A String");

console.log("Result:", output);



/*
run:

function test: 0.023ms
Result: gnirtS A

*/

 



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

/**
 * Measure the time it takes to run a function.
 * Uses console timers for quick debugging.
 */
function timeFunction(label: string, fn: (arg: string) => string, arg: string): string {
  console.time(label);          // start timing
  const result: string = fn(arg);
  console.timeEnd(label);       // end timing and print duration

  return result;
}

// Run the measurement
const output: string = timeFunction("function test", reverseString, "A String");

console.log("Result:", output);


/*
run:

function test: 0.027ms
Result: gnirtS A

*/

 



answered Aug 21 by avibootz
0 votes
/**
 * Reverse a string 
 */

function reverseString (str: string) {
  let reversed: string = '';
 
  for (const character of str) {
    reversed = character + reversed
  }
 
  return reversed
}

/**
 * Measure the time it takes to run a function.
 * Uses console timers for quick debugging.
 */
function timeFunction(label: string, fn: (arg: string) => string, arg: string): string {
  console.time(label);          // start timing
  const result: string = fn(arg);
  console.timeEnd(label);       // end timing and print duration

  return result;
}

// Run the measurement
const output: string = timeFunction("function test", reverseString, "A String");

console.log("Result:", output);


/*
run:

function test: 0.031ms
Result: gnirtS A

*/

 



answered Aug 21 by avibootz
0 votes
/**
 * Reverse a string 
 */

function reverseString (str: string) {
  return [...str].reverse().join('')
}

/**
 * Measure the time it takes to run a function.
 * Uses console timers for quick debugging.
 */
function timeFunction(label: string, fn: (arg: string) => string, arg: string): string {
  console.time(label);          // start timing
  const result: string = fn(arg);
  console.timeEnd(label);       // end timing and print duration

  return result;
}

// Run the measurement
const output: string = timeFunction("function test", reverseString, "A String");

console.log("Result:", output);


/*
run:

function test: 0.015ms
Result: gnirtS A

*/

 



answered Aug 21 by avibootz
...