Skip to content

Instantly share code, notes, and snippets.

@simonlindholm
Created November 30, 2018 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonlindholm/38e1a3ff5d99fdbceda541f112f9daf3 to your computer and use it in GitHub Desktop.
Save simonlindholm/38e1a3ff5d99fdbceda541f112f9daf3 to your computer and use it in GitHub Desktop.
Kattio.js - fast IO for NodeJS
var fs = require("fs");
var Kattio = {
_buf: new Buffer(1 << 14),
_bufPos: 0,
_bufLen: 0,
_ensure: function() {
if (this._bufPos === this._bufLen) {
this._bufPos = 0;
this._bufLen = fs.readSync(0, this._buf, 0, this._buf.length, null);
}
},
_isws: function(ch) {
return ch === 32 || ch === 9 || ch === 10 || ch === 13;
},
_islf: function(ch) {
return ch === 10 || ch === 13;
},
_peekChar: function() {
this._ensure();
return this._bufPos === this._bufLen ? 0 : this._buf[this._bufPos];
},
_skipWs: function() {
while (this._isws(this._peekChar()))
this._bufPos++;
},
_readUntil: function(stop) {
this._ensure();
if (this._bufPos === this._bufLen)
throw new Error("eof");
var start = this._bufPos;
var before = null;
for (;;) {
if (this._bufPos === this._bufLen) {
// Hit the end; need to switch buffers. Thus, stash away all we have so far
// into the 'before' buffer.
var len = this._bufPos - start, preLen = (before ? before.length : 0);
var nbuf = new Buffer(len + preLen);
if (before)
before.copy(nbuf);
before = nbuf;
this._buf.copy(before, preLen, start);
this._ensure();
start = this._bufPos;
}
if (this._bufPos === this._bufLen || stop(this._buf[this._bufPos])) break;
this._bufPos++;
}
if (!before)
return this._buf.toString("utf8", start, this._bufPos);
var after = this._buf.slice(start, this._bufPos);
var res = new Buffer(before.length + after.length);
before.copy(res);
after.copy(res, before.length);
return res.toString();
},
nextToken: function() {
this._skipWs();
return this._readUntil(this._isws);
},
nextLine: function() {
var line = this._readUntil(this._islf);
if (this._peekChar() === 13) this._bufPos++;
if (this._peekChar() === 10) this._bufPos++;
return line;
},
nextNumber: function() { return +this.nextToken(); },
nextInt: function() { return this.nextToken()|0; }
};
// For SpiderMonkey compat
global.readline = function() {
try {
return Kattio.nextLine();
} catch(e) {
return null;
}
};
global.print = function(x) {
console.log(x);
};
// Solution to different
/*
var line;
while (line = readline()) {
var nums = line.split(' ');
var a = parseInt(nums[0]);
var b = parseInt(nums[1]);
print(Math.abs(a - b));
}
*/
// Performance testing: read n > 0 and n integers, and print their sum
var n = Kattio.nextInt(), sum = 0;
for (var i = 0; i < n; i++)
sum += Kattio.nextInt();
console.log(sum);
// Runtime is roughly a factor 4 slower than the following C++ version:
/*
#include <iostream>
using namespace std;
int main() {
cin.sync_with_stdio(false);
long long res = 0;
int n, a;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a;
res += a;
}
cout << res << endl;
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment