Last active
March 1, 2019 21:41
-
-
Save detunized/17f524d74f9fd567d4be7e4a3bbaf1e6 to your computer and use it in GitHub Desktop.
JavaScript solutions profiling
This file contains 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
#!/usr/bin/env node --expose-gc | |
// | |
// Logest word | |
// | |
function findLongestWordLengthForLoop(str) { | |
let maxVal = 0; | |
const wordArr = str.split(' '); | |
for(let i = 0; i < wordArr.length; i++) { | |
let word = wordArr[i]; | |
if (word.length > maxVal) { | |
maxVal = word.length; | |
} | |
} | |
return maxVal; | |
} | |
function findLongestWordLengthForEach(str) { | |
let maxVal = 0; | |
const wordArr = str.split(' '); | |
wordArr.forEach(word => { | |
if (word.length > maxVal) { | |
maxVal = word.length; | |
} | |
}); | |
return maxVal; | |
} | |
function findLongestWordLengthOriginal(str) { | |
const arrOfWords = str.split(' '); | |
const arrOfLengths = arrOfWords.map(item => item.length); | |
return Math.max(...arrOfLengths); | |
} | |
function findLongestWordLengthFixed(str) { | |
const arrOfWords = str.split(' '); | |
const arrOfLengths = arrOfWords.map(item => item.length); | |
let maxLength = 0; | |
for (let i = 0; i < arrOfLengths.length; ++i) { | |
if (arrOfLengths[i] > maxLength) { | |
maxLength = arrOfLengths[i]; | |
} | |
} | |
return maxLength; | |
} | |
function findLongestWordLengthFast(str) { | |
let l = str.length; | |
let maxLength = 0; | |
let currentLength = 0; | |
for (let i = 0; i < l; ++i) { | |
if (str.charCodeAt(i) === 32) { | |
if (currentLength > maxLength) { | |
maxLength = currentLength; | |
} | |
currentLength = 0; | |
} else { | |
++currentLength; | |
} | |
} | |
// Account for the last word | |
return currentLength > maxLength ? currentLength : maxLength; | |
} | |
// | |
// Reverse a string | |
// | |
function reverseStringSubstring(str) { | |
let reversedString = ''; | |
for (let i = str.length; i > 0; i--) { | |
reversedString += str.substring(i, i-1); | |
} | |
return reversedString; | |
} | |
function reverseStringCharAt(str) { | |
let reversedString = ''; | |
for (let i = str.length-1; i >= 0; i--) { | |
reversedString += str.charAt(i); | |
} | |
return reversedString; | |
} | |
function reverseStringSplitJoin(str) { | |
return str.split('').reverse().join(''); | |
} | |
function reverseStringBuffer(str) { | |
let b = Buffer.from(str, "ascii"); | |
for (let i = 0, n = str.length; i < n / 2; i++) { | |
let t = b[i]; | |
b[i] = b[n - 1 - i]; | |
b[n - 1 - i] = t; | |
} | |
return b.toString("ascii"); | |
} | |
function reverseStringBufferReverse(str) { | |
return Buffer | |
.from(str, "ascii") | |
.reverse() | |
.toString("ascii"); | |
} | |
// | |
// Plumbing | |
// | |
function timeIt(label, fs) { | |
const runs = 10; | |
let totals = []; | |
for (let f = 0; f < fs.length; ++f) { | |
global.gc(); | |
totals[f] = 0.0; | |
for (let i = 0; i < runs; ++i) { | |
try | |
{ | |
let start = process.hrtime(); | |
fs[f](); | |
let duraton = process.hrtime(start); | |
totals[f] += duraton[1] / 1000000.0; | |
} | |
catch (e) | |
{ | |
totals[f] = NaN; | |
} | |
} | |
} | |
console.info("%s" + "\t%f".repeat(fs.length), label, ...totals.map(total => total / runs)); | |
} | |
for (let i = 0; i < 1000000; i += 10000) { | |
let str = "a" + " a".repeat(i / 2); | |
let length = str.length; | |
if (0) | |
timeIt(length, [ | |
() => reverseStringSubstring(str), | |
() => reverseStringCharAt(str), | |
() => reverseStringSplitJoin(str), | |
() => reverseStringBuffer(str), | |
() => reverseStringBufferReverse(str), | |
]); | |
if (0) | |
timeIt(length, [ | |
() => findLongestWordLengthForLoop(str), | |
() => findLongestWordLengthForEach(str), | |
() => findLongestWordLengthOriginal(str), | |
() => findLongestWordLengthFixed(str), | |
() => findLongestWordLengthFast(str), | |
]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment