Skip to content

Commit

Permalink
Refactoring (improved performance, but increased memory consumption)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Aug 25, 2020
1 parent adac148 commit ca206e3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 38 deletions.
43 changes: 23 additions & 20 deletions src/stringify-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ const {
}
} = require('./utils');
const noop = () => {};
const needToEscape = /[\x00-\x1f\uD800-\uffff]/;

function quoteJSONString(string) {
return JSON.stringify(string);
function quoteJSONString(str) {
if (needToEscape.test(str)) {
return JSON.stringify(str);
}

return '"' + str + '"';
}

function primitiveToString(value) {
Expand All @@ -24,18 +29,17 @@ function primitiveToString(value) {
return quoteJSONString(value);

case 'number':
return Number.isFinite(value) ? String(value) : 'null';
return Number.isFinite(value) ? value : 'null';

case 'boolean':
return String(value);
return value;

case 'undefined':
case 'object':
return 'null';

default:
// This should never happen, I can't imagine a situation where this executes.
// If you find a way, please open a ticket or PR
// this should never happen
throw new Error(`Unknown type "${typeof value}". Please file an issue!`);
}
}
Expand All @@ -48,16 +52,16 @@ function push() {
function processObjectEntry(key) {
const current = this._stack;

if (current.firstEntry) {
current.firstEntry = false;
if (!current.first) {
current.first = true;
} else {
this.push(',');
}

if (this.space) {
this.push(`\n${this.space.repeat(this._depth)}${quoteJSONString(key)}: `);
} else {
this.push(`${quoteJSONString(key)}:`);
this.push(quoteJSONString(key) + ':');
}
}

Expand All @@ -66,7 +70,7 @@ function processObject() {

// when no keys left, remove obj from stack
if (current.index === current.keys.length) {
if (this.space && !current.firstEntry) {
if (this.space && current.first) {
this.push(`\n${this.space.repeat(this._depth - 1)}}`);
} else {
this.push('}');
Expand All @@ -83,7 +87,7 @@ function processObject() {
}

function processArrayItem(index) {
if (index !== '0') {
if (index !== 0) {
this.push(',');
}

Expand All @@ -106,7 +110,7 @@ function processArray() {
return;
}

this.processValue(String(current.index), current.value[current.index], processArrayItem);
this.processValue(current.index, current.value[current.index], processArrayItem);
current.index++;
}

Expand All @@ -116,21 +120,21 @@ function createStreamReader(fn) {
const data = current.value.read(this._readSize);

if (data !== null) {
current.firstRead = false;
current.first = false;
fn.call(this, data, current);
} else {
if (current.firstRead && !current.value._readableState.reading) {
if (current.first && !current.value._readableState.reading) {
this.popStack();
} else {
current.firstRead = true;
current.first = true;
current.awaiting = true;
}
}
};
}

const processReadableObject = createStreamReader(function(data, current) {
this.processValue(String(current.index), data, processArrayItem);
this.processValue(current.index, data, processArrayItem);
current.index++;
});

Expand Down Expand Up @@ -170,7 +174,7 @@ class JsonStringifyStream extends Readable {
}

if (this.replacer !== null) {
value = this.replacer.call(null, key, value); // FIXME: `this` should be current value
value = this.replacer.call(null, String(key), value); // FIXME: `this` should be current value
}

if (typeof value === 'function' || typeof value === 'symbol') {
Expand Down Expand Up @@ -202,7 +206,7 @@ class JsonStringifyStream extends Readable {
handler: processObject,
value,
index: 0,
firstEntry: true,
first: false,
keys: Object.keys(value)
});
break;
Expand Down Expand Up @@ -268,7 +272,7 @@ class JsonStringifyStream extends Readable {
handler: type === OBJECT_STREAM ? processReadableObject : processReadableString,
value,
index: 0,
firstRead: false,
first: false,
awaiting: !value.readable || value.readableLength === 0
});
const continueProcessing = () => {
Expand Down Expand Up @@ -303,7 +307,6 @@ class JsonStringifyStream extends Readable {

pushStack(node) {
node.prev = this._stack;
node.depth = this._stack ? this._stack.depth + 1 : 0;
return this._stack = node;
}

Expand Down
36 changes: 18 additions & 18 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,35 @@ function isReadableStream(value) {
}

function getTypeNative(value) {
if (value !== null && typeof value === 'object') {
if (Array.isArray(value)) {
return ArrayType;
}
if (value === null || typeof value !== 'object') {
return PrimitiveType;
}

return ObjectType;
if (Array.isArray(value)) {
return ArrayType;
}

return PrimitiveType;
return ObjectType;
}

function getTypeAsync(value) {
if (value !== null && typeof value === 'object') {
if (typeof value.then === 'function') {
return PromiseType;
}
if (value === null || typeof value !== 'object') {
return PrimitiveType;
}

if (isReadableStream(value)) {
return value._readableState.objectMode ? ReadableObjectType : ReadableStringType;
}
if (typeof value.then === 'function') {
return PromiseType;
}

if (Array.isArray(value)) {
return ArrayType;
}
if (isReadableStream(value)) {
return value._readableState.objectMode ? ReadableObjectType : ReadableStringType;
}

return ObjectType;
if (Array.isArray(value)) {
return ArrayType;
}

return PrimitiveType;
return ObjectType;
}

function normalizeReplacer(replacer) {
Expand Down
7 changes: 7 additions & 0 deletions test/stringify-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ describe('stringifyStream()', () => {
'\n', // "\n"
'漢字',
'\u009f', // "\u009f"
'\b\t\n\f\r"\\', // escapes
'\u0000\u0010\u001f\u009f', // "\u009f"
'\uD800\uDC00', // surrogate pair
'\uDC00\uD800', // broken surrogate pair
'\uD800', // leading surrogate (broken surrogate pair)
'\uDC00', // trailing surrogate (broken surrogate pair)
Array.from({ length: 0x900 }).map((_, i) => String.fromCharCode(i)).join(''), // all chars 0x00..0x8FF

// object
{},
Expand Down

0 comments on commit ca206e3

Please sign in to comment.