Skip to content

Instantly share code, notes, and snippets.

@galvez
Created April 4, 2021 02:17
Show Gist options
  • Save galvez/93160cfb327ac540872425a35d9f8654 to your computer and use it in GitHub Desktop.
Save galvez/93160cfb327ac540872425a35d9f8654 to your computer and use it in GitHub Desktop.
const x = [
'header-c',
'value',
'header-a',
'value',
'header-b',
'value',
'header-e',
'value',
'header-d',
'value',
'header-f',
'value'
]
function sort1 (headers) {
const names = []
for (let i = 0; i < headers.length; i += 2) {
names.push(headers[i])
}
names.sort()
const sorted = []
for (const name of names) {
sorted.push(name)
sorted.push(headers[headers.indexOf(name) + 1])
}
return sorted
}
function sort2 (headers) {
let i = headers.length
let previous
while (i -= 2) {
if (headers[i - 2] > headers[i]) {
headers.push(...headers.splice(i - 2, 2))
}
}
}
const funcs = { sort1, sort2 }
for (let i = 0; i < 1000000; i++) {
funcs[process.argv[2]](x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment