Skip to content

Instantly share code, notes, and snippets.

@fabiospampinato
Created May 27, 2021 13:45
Show Gist options
  • Save fabiospampinato/014d15872e2129774ae23783bd377ad2 to your computer and use it in GitHub Desktop.
Save fabiospampinato/014d15872e2129774ae23783bd377ad2 to your computer and use it in GitHub Desktop.
charCodeAt vs TextEncoder benchmark
const fs = require ( 'fs' );
const content = fs.readFileSync ( 'War and Peace.md', 'utf-8' );
const charCodesAt = new Array ( content.length );
console.time('charCodeAt');
for ( let i = 0, l = content.length; i < l; i++ ) {
charCodesAt[i] = content.charCodeAt ( i );
}
console.timeEnd('charCodeAt');
const encoder = new TextEncoder ();
console.time('TextEncoder');
encoder.encode ( content )
console.timeEnd('TextEncoder');
console.log ( content.length );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment