Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

efficiency: short-circuit some and none, fewer not needed intermediate objects #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ http://ricostacruz.com/cheatsheets/umdjs.html
var current;
var scopedLogic;
var scopedData;
var filtered;
var initial;

// easy syntax for unary operators, like {"var" : "x"} instead of strict {"var" : ["x"]}
Expand Down Expand Up @@ -314,7 +313,7 @@ http://ricostacruz.com/cheatsheets/umdjs.html
scopedData = jsonLogic.apply(values[0], data);
scopedLogic = values[1];
// All of an empty set is false. Note, some and none have correct fallback after the for loop
if ( ! scopedData.length) {
if ( ! Array.isArray(scopedData) || ! scopedData.length) {
return false;
}
for (i=0; i < scopedData.length; i+=1) {
Expand All @@ -324,11 +323,31 @@ http://ricostacruz.com/cheatsheets/umdjs.html
}
return true; // All were truthy
} else if (op === "none") {
filtered = jsonLogic.apply({filter: values}, data);
return filtered.length === 0;
scopedData = jsonLogic.apply(values[0], data);
scopedLogic = values[1];

if ( ! Array.isArray(scopedData) || ! scopedData.length) {
return true;
}
for (i=0; i < scopedData.length; i+=1) {
if ( jsonLogic.truthy( jsonLogic.apply(scopedLogic, scopedData[i]) )) {
return false; // First truthy, short circuit
}
}
return true; // None were truthy
} else if (op === "some") {
filtered = jsonLogic.apply({filter: values}, data);
return filtered.length > 0;
scopedData = jsonLogic.apply(values[0], data);
scopedLogic = values[1];

if ( ! Array.isArray(scopedData) || ! scopedData.length) {
return false;
}
for (i=0; i < scopedData.length; i+=1) {
if ( jsonLogic.truthy( jsonLogic.apply(scopedLogic, scopedData[i]) )) {
return true; // First truthy, short circuit
}
}
return false; // None were truthy
}

// Everyone else gets immediate depth-first recursion
Expand All @@ -346,7 +365,6 @@ http://ricostacruz.com/cheatsheets/umdjs.html
var sub_ops = String(op).split(".");
var operation = operations;
for (i = 0; i < sub_ops.length; i++) {

if (!operation.hasOwnProperty(sub_ops[i])) {
throw new Error("Unrecognized operation " + op +
" (failed at " + sub_ops.slice(0, i+1).join(".") + ")");
Expand Down Expand Up @@ -377,7 +395,7 @@ http://ricostacruz.com/cheatsheets/umdjs.html
collection.push(values[0]);
} else {
// Recursion!
values.map(function(val) {
values.forEach(function(val) {
collection.push.apply(collection, jsonLogic.uses_data(val) );
});
}
Expand Down