Created
January 11, 2020 12:11
-
-
Save capJavert/9b9cdfbcf8cd27cceee078e9d1b7df51 to your computer and use it in GitHub Desktop.
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
const fetch = require('isomorphic-unfetch') | |
const { performance } = require('perf_hooks') | |
//Replace char function | |
function setCharAt(str,index,chr) { | |
if(index > str.length-1) return str; | |
return str.substr(0,index) + chr + str.substr(index+1); | |
} | |
//Check if they are the same | |
function elIsto (a,b){ | |
//Check length first | |
if (a.length != b.length) return false | |
for ( var i=0 ; i < a.length ; i++ ){ | |
// console.log( 'a=' + i ) | |
for ( var j= 0 ; j < b.length ; j++ ){ | |
// console.log( 'b=' + j ) | |
// console.log(b) | |
if ( a[i] === b[j] ){ | |
b = setCharAt(b,j,'') | |
break | |
} | |
else if ( j === (b.length - 1) ) return false | |
} | |
} | |
return true | |
} | |
const isAnagram = (a, b) => { | |
if (a.length !== b.length) { | |
return false | |
} | |
return a.split('').sort().join('') === b.split('').sort().join('') | |
} | |
const original = 'darkotorbasinovic' | |
const getData = async () => { | |
return (await fetch('https://pastebin.com/raw/8mm3sF46').then(res => res.text())).split(/\r?\n/).map(anagram => { | |
return anagram.replace(/\s/g, '').toLowerCase() // remove all spaces and make everything lowercase | |
}) | |
} | |
const results = { | |
elIsto: [], | |
isAnagram: [] | |
} | |
const averageResult = arr => arr.reduce((a,b) => a + b, 0) / arr.length | |
const sumResult = arr => arr.reduce((a,b) => a + b, 0) | |
getData().then(data => { | |
for (let i = 0; i < 100; i += 1) { | |
const elIsto0 = performance.now() | |
data.forEach(anagram => elIsto(original, anagram)) | |
const elIsto1 = performance.now() | |
results.elIsto.push(elIsto1 - elIsto0) | |
const isAnagram0 = performance.now() | |
data.forEach(anagram => isAnagram(original, anagram)) | |
const isAnagram1 = performance.now() | |
results.isAnagram.push(isAnagram1 - isAnagram0) | |
} | |
console.log('elIsto') | |
console.log('average', averageResult(results.elIsto), 'ms') | |
console.log('min', Math.min(...results.elIsto), 'ms') | |
console.log('max', Math.max(...results.elIsto), 'ms') | |
console.log('total', sumResult(results.elIsto), 'ms') | |
console.log('isAnagram') | |
console.log('average', averageResult(results.isAnagram), 'ms') | |
console.log('min', Math.min(...results.isAnagram), 'ms') | |
console.log('max', Math.max(...results.isAnagram), 'ms') | |
console.log('total', sumResult(results.isAnagram), 'ms') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment