question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sub-optimal boolean returns

See original GitHub issue

Input:

function isTextInputElement(elem) {
  if (!elem) {
    return false;
  }

  if (elem.nodeName === 'INPUT') {
    return !!supportedInputTypes[elem.type];
  }

  if (elem.nodeName === 'TEXTAREA') {
    return true;
  }

  return false;
}

GCC Output:

function(a){return a?"INPUT"===a.nodeName?!!supportedInputTypes[a.type]:"TEXTAREA"===a.nodeName?!0:!1:!1}

Two things to be noted:

  • if (a === b) { return true; } return false; is outputted as a === b ? !0 : !1 which can be simplified as a === b, no need to do the condition.
  • all the return values have ! in common, it is possible to factor it out. return !(a?"INPUT"===a.nodeName?!supportedInputTypes[a.type]:"TEXTAREA"===a.nodeName?0:1:1)

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vjeuxcommented, Jul 6, 2016

I modified the benchmark script a bit

diff --git a/scripts/benchmark.js b/scripts/benchmark.js
index 3f17b38..5c9c2f9 100755
--- a/scripts/benchmark.js
+++ b/scripts/benchmark.js
@@ -17,30 +17,42 @@ if (!filename) {
   process.exit(1);
 }

+const option = process.argv[3];
+const showCode = (option === '--code');
+
+const tableChars = {
+  top: '',
+  'top-mid': '' ,
+  'top-left': '' ,
+  'top-right': '',
+  bottom: '' ,
+  'bottom-mid': '' ,
+  'bottom-left': '' ,
+  'bottom-right': '',
+  left: '',
+  'left-mid': '',
+  mid: '',
+  'mid-mid': '',
+  right: '' ,
+  'right-mid': '',
+  middle: ' ',
+};
+const tableStyle = {
+  'padding-left': 0,
+  'padding-right': 0,
+  head: ['bold'],
+};
+
 const table = new Table({
   head: ['', 'raw', 'raw win', 'gzip', 'gzip win', 'parse time', 'run'],
-  chars: {
-    top: '',
-    'top-mid': '' ,
-    'top-left': '' ,
-    'top-right': '',
-    bottom: '' ,
-    'bottom-mid': '' ,
-    'bottom-left': '' ,
-    'bottom-right': '',
-    left: '',
-    'left-mid': '',
-    mid: '',
-    'mid-mid': '',
-    right: '' ,
-    'right-mid': '',
-    middle: ' ',
-  },
-  style: {
-    'padding-left': 0,
-    'padding-right': 0,
-    head: ['bold'],
-  },
+  chars: tableChars,
+  style: tableStyle,
+});
+
+const codeTable = new Table({
+  head: ['', 'code'],
+  chars: tableChars,
+  style: tableStyle,
 });

 const results = [];
@@ -70,6 +82,7 @@ function test(name, callback) {
     gzip: gzipped.length,
     parse: parseNow,
     run: run,
+    code: result.toString(),
   });
 }

@@ -116,6 +129,11 @@ results = results.sort(function (a, b) {
 });

 results.forEach(function (result, i) {
+  codeTable.push([
+    chalk.bold(result.name),
+    result.code,
+  ]);
+
   const row = [
     chalk.bold(result.name),
     bytes(result.raw),
@@ -123,7 +141,7 @@ results.forEach(function (result, i) {
     bytes(result.gzip),
     Math.round(((gzippedCode.length / result.gzip) * 100) - 100) + '%',
     Math.round(result.parse) + 'ms',
-    Math.round(result.run) + 'ms',
+    Math.round(result.run) + 'ms'
   ];

   const style = chalk.yellow;
@@ -141,3 +159,7 @@ results.forEach(function (result, i) {
 });

 console.log(table.toString());
+if (showCode) {
+  console.log();
+  console.log(codeTable.toString());
+}
0reactions
kangaxcommented, Oct 26, 2016

Our output seems to be a little better now, but still not as good as closure:

function isTextInputElement(a){return!!a&&('INPUT'===a.nodeName?!!supportedInputTypes[a.type]:!('TEXTAREA'!==a.nodeName))}
Read more comments on GitHub >

github_iconTop Results From Across the Web

48297 – Suboptimal optimization of boolean expression addition
gcc does not seem to recognize that boolean expressions, such as (a==b), are at most 1. At least not when performing math on...
Read more >
A* algorithm completes but returns suboptimal path
The algorithm: Uses a PQ that supports change priority operations. Assume all the ADTs work correctly. Example problem: Find the shortest path ...
Read more >
IBM2804: suboptimal compares
A Boolean is a result of a comparison of two expressions or the result of anding, oring or negating Booleans. As such, a...
Read more >
Comparing Hybrid Property to Boolean results in sub-optimal ...
Interesting, the suggestion that a boolean expression when compared to True should implicitly omit the True (and I suppose when compared to False...
Read more >
EE364a Homework 5 solutions
We refer to this problem as the LP relaxation of the Boolean LP (4.67). The LP ... In scenario j, the return for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found