Skip to content

Instantly share code, notes, and snippets.

@kyr0
Created March 24, 2019 08:18
Show Gist options
  • Save kyr0/144e7ea894db671dae11c3867e99ec81 to your computer and use it in GitHub Desktop.
Save kyr0/144e7ea894db671dae11c3867e99ec81 to your computer and use it in GitHub Desktop.
wp-node.js
'use strict';
const fs = require('fs');
const startTime = Date.now();
const log = (msg) => setImmediate(() => { process.stdout.write(msg); });
fs.readFile("/dev/stdin", 'utf8', (err, text) => {
const map = {}, sorted = [], lines = text.split("\n");
for (let i=0; i<lines.length; i++) {
const words = lines[i].split(' ');
for (let j=0; j<words.length; j++) {
if (words[j].length === 0) continue;
if (typeof map[words[j]] === 'undefined') map[words[j]] = 1;
else map[words[j]]++;
}
}
for (let word in map) sorted.push([word, map[word]]);
sorted.sort((a, b) => b[1] - a[1]);
for (let i=0; i<sorted.length; i++) log(`${sorted[i][0]}: ${sorted[i][1]}\n`);
const endTime = Date.now();
log(`Time: ${endTime - startTime}ms\n`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment