Created
April 30, 2020 21:44
-
-
Save shettayyy/6769bec6ed4efca4f4b4caccfa25d4d6 to your computer and use it in GitHub Desktop.
Check the performance of the function that sums 'n' numbers using loop and math// source https://jsbin.com/tajiyoq
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="Check the performance of the function that sums 'n' numbers using loop and math"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
"use strict"; | |
var loopSum = function loopSum(n) { | |
var sum = 0; | |
for (var i = 1; i <= n; i++) { | |
sum += i; | |
} | |
return sum; | |
}; | |
var t1 = performance.now(); | |
var sum1 = loopSum(100000); | |
var t2 = performance.now(); | |
console.log("t1", t1, "t2", t2, "total sec", (t2 - t1) / 1000, "sum1", sum1); | |
var mathSum = function mathSum(n) { | |
return n * (n + 1) / 2; | |
}; | |
var t3 = performance.now(); | |
var sum2 = mathSum(100000); | |
var t4 = performance.now(); | |
console.log("t3", t3, "t4", t4, "total sec", (t4 - t3) / 1000, "sum2", sum2); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const loopSum = (n) => { | |
let sum = 0; | |
for(let i = 1; i <= n; i++) { | |
sum += i; | |
} | |
return sum; | |
}; | |
const t1 = performance.now(); | |
const sum1 = loopSum(100000); | |
const t2 = performance.now(); | |
console.log("t1", t1, "t2", t2, "total sec", (t2 - t1)/1000, "sum1", sum1); | |
const mathSum = (n) => { | |
return (n * (n + 1)) / 2; | |
}; | |
const t3 = performance.now(); | |
const sum2 = mathSum(100000); | |
const t4 = performance.now(); | |
console.log("t3", t3, "t4", t4, "total sec", (t4 - t3)/1000, "sum2", sum2);</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var loopSum = function loopSum(n) { | |
var sum = 0; | |
for (var i = 1; i <= n; i++) { | |
sum += i; | |
} | |
return sum; | |
}; | |
var t1 = performance.now(); | |
var sum1 = loopSum(100000); | |
var t2 = performance.now(); | |
console.log("t1", t1, "t2", t2, "total sec", (t2 - t1) / 1000, "sum1", sum1); | |
var mathSum = function mathSum(n) { | |
return n * (n + 1) / 2; | |
}; | |
var t3 = performance.now(); | |
var sum2 = mathSum(100000); | |
var t4 = performance.now(); | |
console.log("t3", t3, "t4", t4, "total sec", (t4 - t3) / 1000, "sum2", sum2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment