ProTip: return vs. exit() - One Way is Way Better

protip
optimization
triggers

#1

tl:dr; use return;


There are two ways to quit a trigger:

  • exit();

  • return;

Both work, one is measurably better than the other.

A Real World Example

We deployed a bundle I wrote (SSL Expiration) and watched it work.

There are two code fragments in the bundle that caught my eye:

(SSL.certificate === null) & exit();

and

(subjectsOfInterest.test(subject)) || exit();

We tweaked those lines to use return() and ended up with this:

if ( ! SSL.certificate ) return;

and

if ( ! subjectsOfInterest.test(subject)) return;

Here are the results:

That inflection point at 16:03 is when we change from exit();   to   return;.

That’s a drop from about 130,000 trigger cycles to about 100,000 trigger cycles.

That’s a 23% drop.

When you are seeing 40Gbps and decrypting 64,000 SSL handshakes per second, improvements like this matter.


#2

I had somebody ask me about this code fragment:

if ( ! SSL.certificate ) return;

That’s valid syntax and is a short way of saying this:

if ( ! SSL.certificate ) {
    return;
}

Same thing, one is shorter, and a wee bit easier to read.


#3

Blog post on the same subject (including video!)

https://www.extrahop.com/community/blog/2015/trigger-performance-optimization-exit-vs-return/