Created
January 25, 2024 13:16
-
-
Save ribomation/a8108cc87748a2600c209fe20b9e0c6b to your computer and use it in GitHub Desktop.
1BRC JavaScript / Node.js
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
import {createReadStream} from 'node:fs'; | |
import * as readline from 'node:readline/promises'; | |
const start_time = process.hrtime(); | |
const data = new Map(); | |
const filename = process.argv[2] || '../../../data/weather-data-1M.csv'; | |
console.log('filename: %s\n----', filename); | |
const fileStream = createReadStream(filename); | |
const rl = readline.createInterface({ | |
input: fileStream, | |
crlfDelay: Infinity, | |
}); | |
for await (const line of rl) { | |
let [name, temp] = line.split(';'); | |
temp = parseFloat(temp); | |
const next = {count: 1, sum: temp, min: temp, max: temp}; | |
const a = data.get(name); | |
if (!!a) { | |
next.count = 1 + a.count; | |
next.sum += a.sum; | |
next.min = Math.min(temp, a.min); | |
next.max = Math.max(temp, a.max); | |
} | |
data.set(name, next); | |
} | |
const sortable = [...data.entries()]; | |
sortable.sort((a, b) => a[0].localeCompare(b[0])) | |
sortable.forEach(([name, aggr]) => { | |
console.log('%s: %f C, %f/%f (%d)', name, | |
(aggr.sum / aggr.count).toFixed(1), | |
(aggr.min).toFixed(1), | |
(aggr.max).toFixed(1), | |
aggr.count) | |
}) | |
console.log('----') | |
const [secs, nano] = process.hrtime(start_time); | |
const elapsed = (secs + nano * 1E-9).toFixed(3) | |
process.stderr.write('[js await] elapsed ' + elapsed + ' seconds, ' + filename + '\n') |
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
import {createReadStream} from 'node:fs'; | |
import * as readline from 'node:readline/promises'; | |
const start_time = process.hrtime(); | |
const data = new Map(); | |
const filename = process.argv[2] || '../../../data/weather-data-1M.csv'; | |
console.log('filename: %s\n----', filename); | |
const fileStream = createReadStream(filename); | |
const rl = readline.createInterface({ | |
input: fileStream, | |
crlfDelay: Infinity, | |
}); | |
rl.on('line', line => { | |
let [name, temp] = line.split(';'); | |
temp = parseFloat(temp); | |
const next = {count: 1, sum: temp, min: temp, max: temp}; | |
const a = data.get(name); | |
if (!!a) { | |
next.count = 1 + a.count; | |
next.sum += a.sum; | |
next.min = Math.min(temp, a.min); | |
next.max = Math.max(temp, a.max); | |
} | |
data.set(name, next); | |
}); | |
rl.on('close', () => { | |
let sortable = [...data.entries()]; | |
sortable.sort((a, b) => a[0].localeCompare(b[0])) | |
sortable.forEach(([name, aggr]) => { | |
console.log('%s: %f C, %f/%f (%d)', name, | |
(aggr.sum / aggr.count).toFixed(1), | |
(aggr.min).toFixed(1), | |
(aggr.max).toFixed(1), | |
aggr.count) | |
}) | |
console.log('----') | |
const [secs, nano] = process.hrtime(start_time); | |
const elapsed = (secs + nano * 1E-9).toFixed(3) | |
process.stderr.write('[js await] elapsed ' + elapsed + ' seconds, ' + filename + '\n') | |
}); | |
rl.on('error', err => { | |
console.error('ooops:', err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment