Skip to content

Commit 0c7a2f0

Browse files
🚀 perf(_cmp): Use one less comparison.
1 parent 359276c commit 0c7a2f0

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

src/cmp.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function _cmp ( { jz , lt0 , lt , neg , divmod } ) {
1+
export function _cmp ( { jz , lt0 , cmp , neg , divmod } ) {
22

33
const compare_positive_fractions = function (a,b,c,d) {
44
if (jz(b)) {
@@ -8,12 +8,10 @@ export function _cmp ( { jz , lt0 , lt , neg , divmod } ) {
88
if (jz(d)) return -1;
99
const [ q1 , r1 ] = divmod(a, b) ;
1010
const [ q2 , r2 ] = divmod(c, d) ;
11-
if (lt(q1,q2)) return -1;
12-
if (lt(q2,q1)) return 1;
13-
return compare_positive_fractions(d,r2,b,r1);
11+
return cmp(q1, q2) || compare_positive_fractions(d,r2,b,r1);
1412
}
1513

16-
const cmp = function (a,b,c,d) {
14+
return function (a,b,c,d) {
1715
if (lt0(b)) { b = neg(b); a = neg(a); }
1816
if (lt0(d)) { d = neg(d); c = neg(c); }
1917
if (lt0(a)) {
@@ -24,6 +22,4 @@ export function _cmp ( { jz , lt0 , lt , neg , divmod } ) {
2422
return compare_positive_fractions(a,b,c,d);
2523
}
2624

27-
return cmp ;
28-
2925
}

test/src/core.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const ALU = [
4040
str : x => x.toString(),
4141
jz : x => x.eq(0),
4242
lt0 : x => x.lt(0),
43-
lt : (a,b) => a.lt(b),
43+
cmp : (a,b) => a.cmp(b),
4444
neg : x => x.neg(),
4545
divmod : (a,b) => [a.div(b), a.mod(b)],
4646
},
@@ -54,7 +54,7 @@ const ALU = [
5454
str : x => x.toString(),
5555
jz : x => x.iszero(),
5656
lt0 : x => x.sign() < 0,
57-
lt : (a,b) => a.lt(b),
57+
cmp : (a,b) => a.cmp(b),
5858
neg : x => x.opposite(),
5959
divmod : (a,b) => a.divmod(b),
6060
}

0 commit comments

Comments
 (0)