Skip to content

Commit b82d167

Browse files
improve performance of sequence wrapper
1 parent 64baffe commit b82d167

File tree

3 files changed

+16
-57
lines changed

3 files changed

+16
-57
lines changed

src/functions.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ const functions = (() => {
1313
var isArrayOfStrings = utils.isArrayOfStrings;
1414
var isArrayOfNumbers = utils.isArrayOfNumbers;
1515
var createSequence = utils.createSequence;
16-
// var isSequence = utils.isSequence;
17-
// var toSequence = utils.toSequence;
1816
var isFunction = utils.isFunction;
1917
var isLambda = utils.isLambda;
2018
var isIterable = utils.isIterable;

src/jsonata.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ var jsonata = (function() {
2929
var isArrayOfNumbers = utils.isArrayOfNumbers;
3030
var createSequence = utils.createSequence;
3131
var isSequence = utils.isSequence;
32-
var toSequence = utils.toSequence;
3332
var isFunction = utils.isFunction;
3433
var isLambda = utils.isLambda;
3534
var isIterable = utils.isIterable;
@@ -113,11 +112,11 @@ var jsonata = (function() {
113112
break;
114113
}
115114

116-
if(environment.lookup('__jsonata_async') &&
115+
if(environment.async &&
117116
(typeof result === 'undefined' || result === null || typeof result.then !== 'function')) {
118117
result = Promise.resolve(result);
119118
}
120-
if(environment.lookup('__jsonata_async') && typeof result.then === 'function' && expr.nextFunction && typeof result[expr.nextFunction] === 'function') {
119+
if(environment.async && typeof result.then === 'function' && expr.nextFunction && typeof result[expr.nextFunction] === 'function') {
121120
// although this is a 'thenable', it is chaining a different function
122121
// so don't yield since yielding will trigger the .then()
123122
} else {
@@ -142,7 +141,12 @@ var jsonata = (function() {
142141
if(expr.keepArray) {
143142
result.keepSingleton = true;
144143
}
145-
result = result.value();
144+
if(result.length === 0) {
145+
result = undefined;
146+
} else if(result.length === 1) {
147+
result = result.keepSingleton ? result : result[0];
148+
}
149+
146150
}
147151

148152
return result;
@@ -849,7 +853,8 @@ var jsonata = (function() {
849853
for (var item = lhs, index = 0; item <= rhs; item++, index++) {
850854
result[index] = item;
851855
}
852-
return toSequence(result);
856+
result.sequence = true;
857+
return result;
853858
}
854859

855860
/**
@@ -1606,7 +1611,8 @@ var jsonata = (function() {
16061611
}
16071612
return value;
16081613
},
1609-
timestamp: enclosingEnvironment ? enclosingEnvironment.timestamp : null
1614+
timestamp: enclosingEnvironment ? enclosingEnvironment.timestamp : null,
1615+
async: enclosingEnvironment ? enclosingEnvironment.async : false
16101616
};
16111617
}
16121618

@@ -1854,7 +1860,7 @@ var jsonata = (function() {
18541860
var result, it;
18551861
// if a callback function is supplied, then drive the generator in a promise chain
18561862
if(typeof callback === 'function') {
1857-
exec_env.bind('__jsonata_async', true);
1863+
exec_env.async = true;
18581864
var catchHandler = function (err) {
18591865
populateMessage(err); // possible side-effects on `err`
18601866
callback(err, null);

src/utils.js

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ const utils = (() => {
5959
* @returns {Array} - empty sequence
6060
*/
6161
function createSequence() {
62-
var sequence = toSequence([]);
62+
var sequence = [];
63+
sequence.sequence = true;
6364
if (arguments.length === 1) {
6465
sequence.push(arguments[0]);
6566
}
@@ -72,52 +73,7 @@ const utils = (() => {
7273
* @returns {boolean} true if it's a sequence
7374
*/
7475
function isSequence(value) {
75-
var sequence = value.sequence;
76-
if(sequence === true) {
77-
var desc = Object.getOwnPropertyDescriptor(value, 'sequence');
78-
if(desc.enumerable === false) {
79-
return true;
80-
}
81-
}
82-
return false;
83-
}
84-
85-
/**
86-
* Converts an array to a result sequence (by adding special properties)
87-
* @param {Array} arr - the array to convert
88-
* @returns {*} - the sequence
89-
*/
90-
function toSequence(arr) {
91-
Object.defineProperty(arr, 'sequence', {
92-
enumerable: false,
93-
configurable: false,
94-
get: function () {
95-
return true;
96-
}
97-
});
98-
Object.defineProperty(arr, 'keepSingleton', {
99-
enumerable: false,
100-
configurable: false,
101-
writable: true,
102-
value: false
103-
});
104-
Object.defineProperty(arr, 'value', {
105-
enumerable: false,
106-
configurable: false,
107-
get: function () {
108-
return function() {
109-
switch (this.length) {
110-
case 0:
111-
return undefined;
112-
case 1:
113-
return this.keepSingleton ? this : this[0];
114-
default:
115-
return this;
116-
}
117-
};
118-
}
119-
});
120-
return arr;
76+
return value.sequence === true && Array.isArray(value);
12177
}
12278

12379
/**
@@ -175,7 +131,6 @@ const utils = (() => {
175131
isArrayOfNumbers,
176132
createSequence,
177133
isSequence,
178-
toSequence,
179134
isFunction,
180135
isLambda,
181136
isIterable,

0 commit comments

Comments
 (0)