Skip to content

Commit b027b7d

Browse files
committed
Use specialized methods for comparisons
Remove the generalized `cast` in favor of 3 specialized ones. This is a slight optimizations for comparisons.
1 parent 4d5a7b7 commit b027b7d

File tree

2 files changed

+55
-22
lines changed

2 files changed

+55
-22
lines changed

src/compiler/blocks/compiler_scratch3_operators.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,19 @@ module.exports.getInputs = () => {
4040
const equals = /** @param {InputUtil} util */ (util) => {
4141
const OPERAND1 = util.input('OPERAND1');
4242
const OPERAND2 = util.input('OPERAND2');
43-
return util.boolean(`(compare(${OPERAND1}, ${OPERAND2}) === 0)`);
43+
return util.boolean(`compareEqual(${OPERAND1}, ${OPERAND2})`);
4444
};
4545

4646
const greaterThan = /** @param {InputUtil} util */ (util) => {
4747
const OPERAND1 = util.input('OPERAND1');
4848
const OPERAND2 = util.input('OPERAND2');
49-
return util.boolean(`(compare(${OPERAND1}, ${OPERAND2}) > 0)`);
49+
return util.boolean(`compareGreaterThan(${OPERAND1}, ${OPERAND2})`);
5050
};
5151

5252
const lessThan = /** @param {InputUtil} util */ (util) => {
5353
const OPERAND1 = util.input('OPERAND1');
5454
const OPERAND2 = util.input('OPERAND2');
55-
return util.boolean(`(compare(${OPERAND1}, ${OPERAND2}) < 0)`);
55+
return util.boolean(`compareLessThan(${OPERAND1}, ${OPERAND2})`);
5656
};
5757

5858
const and = /** @param {InputUtil} util */ (util) => {
@@ -134,6 +134,7 @@ const mathop = /** @param {InputUtil} util */ (util) => {
134134
const random = /** @param {InputUtil} util */ (util) => {
135135
const FROM = util.input('FROM');
136136
const TO = util.input('TO');
137+
// If we know the high and low values are compile-time, we can determine whether to return ints or floats at compile time.
137138
if (typeof FROM.constantValue !== 'undefined' && typeof TO.constantValue !== 'undefined') {
138139
const nFrom = Cast.toNumber(FROM.constantValue);
139140
const nTo = Cast.toNumber(TO.constantValue);

src/compiler/execute.js

+51-19
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,15 @@ const toBoolean = (value) => {
179179
*/
180180
const isWhiteSpace = (val) => {
181181
return val === null || (typeof val === 'string' && val.trim().length === 0);
182-
}
182+
};
183183

184184
/**
185-
* Compare two values using Scratch casting.
186-
* Similar to Cast.compare()
187-
* @param {*} v1 First value to compare.
188-
* @param {*} v2 Second value to compare.
189-
* @returns {number} Negative if v1 < v2, 0 if equal, positive if v1 > v2
185+
* Determine if two values are equal.
186+
* @param {*} v1
187+
* @param {*} v2
188+
* @returns {boolean} true if v1 is equal to v2
190189
*/
191-
const compare = (v1, v2) => {
190+
const compareEqual = (v1, v2) => {
192191
let n1 = +v1;
193192
let n2 = +v2;
194193
if (n1 === 0 && isWhiteSpace(v1)) {
@@ -199,20 +198,53 @@ const compare = (v1, v2) => {
199198
if (isNaN(n1) || isNaN(n2)) {
200199
const s1 = ('' + v1).toLowerCase();
201200
const s2 = ('' + v2).toLowerCase();
202-
if (s1 < s2) {
203-
return -1;
204-
} else if (s1 > s2) {
205-
return 1;
206-
}
207-
return 0;
201+
return s1 === s2;
202+
}
203+
return n1 === n2;
204+
};
205+
206+
/**
207+
* Determine if one value is greater than another.
208+
* @param {*} v1
209+
* @param {*} v2
210+
* @returns {boolean} true if v1 is greater than v2
211+
*/
212+
const compareGreaterThan = (v1, v2) => {
213+
let n1 = +v1;
214+
let n2 = +v2;
215+
if (n1 === 0 && isWhiteSpace(v1)) {
216+
n1 = NaN;
217+
} else if (n2 === 0 && isWhiteSpace(v2)) {
218+
n2 = NaN;
219+
}
220+
if (isNaN(n1) || isNaN(n2)) {
221+
const s1 = ('' + v1).toLowerCase();
222+
const s2 = ('' + v2).toLowerCase();
223+
return s1 > s2;
224+
}
225+
return n1 > n2;
226+
};
227+
228+
/**
229+
* Determine if one value is less than another.
230+
* @param {*} v1
231+
* @param {*} v2
232+
* @returns {boolean} true if v1 is less than v2
233+
*/
234+
const compareLessThan = (v1, v2) => {
235+
let n1 = +v1;
236+
let n2 = +v2;
237+
if (n1 === 0 && isWhiteSpace(v1)) {
238+
n1 = NaN;
239+
} else if (n2 === 0 && isWhiteSpace(v2)) {
240+
n2 = NaN;
208241
}
209-
if (
210-
(n1 === Infinity && n2 === Infinity) ||
211-
(n1 === -Infinity && n2 === -Infinity)
212-
) {
213-
return 0;
242+
if (isNaN(n1) || isNaN(n2)) {
243+
const s1 = ('' + v1).toLowerCase();
244+
const s2 = ('' + v2).toLowerCase();
245+
return s1 < s2;
214246
}
215-
return n1 - n2;
247+
return n1 < n2;
216248
};
217249

218250
/**

0 commit comments

Comments
 (0)