Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arccoza/3561fcaf6fef985c4c123759c7b414c1 to your computer and use it in GitHub Desktop.
Save arccoza/3561fcaf6fef985c4c123759c7b414c1 to your computer and use it in GitHub Desktop.
javascript: convert string to array of chars performance created by arccoza - https://repl.it/IWyr/6
var str = `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
進善公愛載懸千間前死断直周税。稿実写使松出子府上難向飾幹超。属三闘与結社京政郎回終線見権競式部文賀帰。星画直小和氏路芸間提別対人面。年蹊雪北聞詐節禁画藤社念歳歴歳子読及。績転席上席作市放将図教海的横表載脚大。話能求全耳北時議定著測質稲護紀。農済哉無総図出悲質情容経。以野態自亡必員災博新非脳察趣着車。`
var arr = null
var tests = {
'Object.assign'(its) {
for(let i = 0; i < its; i++)
arr = Object.assign([], str)
},
'Array.from'(its) {
for(let i = 0; i < its; i++)
arr = Array.from(str)
},
'regex'(its) {
for(let i = 0; i < its; i++)
arr = str.split(/(?=.)/u)
},
'spread'(its) {
for(let i = 0; i < its; i++)
arr = [...str]
}
}
function run(its=1000) {
var results = {}
for(let test in tests) {
let start = performance.now()
tests[test](its)
results[test] = performance.now() - start
}
return results
}
console.log(run(5000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment