Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
Why is there a difference in time complexity in functions of objects and stand alone functions

I tried comparing two functions which perform the same task:

Number.prototype.getReverse = function() {
  let reverseNum = 0
  let num = this
  while(num > 0) {
    reverseNum = (10 * reverseNum) + (num % 10)
    num = Math.floor(num / 10)
  }
  return reverseNum
}
const numb = 1234567
console.time()
console.log(numb.getReverse())
console.timeEnd()

function getReverseNum(num) {
  let reverseNum = 0
  while (num > 0) {
    reverseNum = (10 * reverseNum) + (num % 10)
    num = Math.floor(num / 10)
  }
  return reverseNum
}

console.time()
console.log(getReverseNum(numb))
console.timeEnd()

The prototype of the Number object took far longer:

7654321
default: 0.42ms
7654321
default: 0.109ms

I was curious as to whether anyone might know why this is. Does it have to do with the way JavaScript accesses the function through the object Number?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!