Skip to content

Instantly share code, notes, and snippets.

@oleh-zaporozhets
Created March 9, 2020 16:07
Show Gist options
  • Save oleh-zaporozhets/e6badd8012d013a0aba4c2be3787bbdf to your computer and use it in GitHub Desktop.
Save oleh-zaporozhets/e6badd8012d013a0aba4c2be3787bbdf to your computer and use it in GitHub Desktop.
const fs = require('fs');
const str = fs.readFileSync('./str.txt', 'utf8');
console.log('String length:', str.length);
console.log('---------');
function test() {
console.time('Loop speed');
let res = '';
for (let index = 0; index < str.length; index++) {
res += str[index] + ' ';
}
const used = process.memoryUsage().heapUsed / 1024 / 1024;
console.log(`Loop uses approximately ${Math.round(used * 100) / 100} MB`);
console.timeEnd('Loop speed');
console.log('---------');
}
for (let index = 0; index < 10; index++) {
test();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment