I was asked "how to check for a string contains a value (for setting an analytics variable) and what would be the appropriate thing to use?" I recommended readability over the regex solution hands down. Here's why...


Tag management and analytics is largely used for those with marketing backgrounds trying to improve upon some sort of process (sales flows, visits, promotions, etc.). When that is the largest user base, many of the tools out there try to make the implementations as easy as possible, because not everyone knows how to code their own solutions. So, when I'm asked how to implement some sort of custom logic, my first response is always "make it as readable as possible."

With the question above, there's two common solutions that people may use:

  1. Regex. For example: if (/\/consumer\//i.test(url)) { alert('matched'); }
  2. JS string methods. Same example: if (url.toLowerCase().indexOf('/consumer/')>-1) { alert('matched'); }

#1 is going to be slightly faster because js regex expression testing is quicker than calling functions. However, #2 is more manageable because we can directly see we are checking a lowercase value of "/consumer/". I will always recommend the second solution though and here's why:

Readability is much more useful in the long run.

There's always sayings to document code things as if the next person knew nothing about the code. This is repeated so often because readability of code is so important to help others know what your code is doing. Taking my preface above, you can see why it is even more important in tag management or analytics - namely, the person following you may have NO code experience, so readability is crucial. They need to be able to update your code to match new requirements, so it is your responsibility to make it as doable as possible. Looking at our examples above, a developer would have no problem with either solution. However, if you've never developed before, you're going to be looking for key words to help you understand what is going on. Several things in the second solution are much easier to understand in that case:

  • toLowerCase or /i - The former is much easier to know that I'm checking for a lowercase value
  • '/consumer/' or \/consumer\/ - The former is much easier to see the exact value I'm looking for
  • indexOf>-1 or .test - both of these are equally difficult in context. Mostly because indexOf doesn't tell me exactly what it is doing (if I'm not a developer), and .test would be clear, but the ordering doesn't make much sense (url.test(pattern) would make more grammatical sense).
  • If statement result - The indexOf>-1 is a very clear true/false type of outcome. However, .test doesn't tell me if the actual result is a boolean, null value, undefined, empty string, or array.

Speed is negligible

Regex can be much faster in javascript for testing statements. However, we're testing a single url for analytics and tag management in this scenario, not processing and checking thousands of lines of data. This is always the case in this field of work, so there's no need to do the micro speed optimizations that #1 over #2 will provide.

Most tools are going to already be optimized in the code base (minifying, or at least condensing), so it just makes sense to do readability over speed. I've never come across a single scenario in hundreds of complex implementations where speed over readability made sense. You could argue this point all day long, but in the end, the next person following you that can't read code is going to be asking you for clarification. This will require more time (thus money) that could have been better spent elsewhere.


I'm throwing this in here because, as any experienced developer knows, it is more professional to do better planning and documentation. When I first started out with tag management and analytics - I was always doing more optimized expressions (like regex). However, the more clients I worked with, including full development companies - I just kept seeing that people would get confused over the littlest of things. I would get lots of complaints or questions that took up a lot of time to respond to that could have been resolved by making my implementations more readable.

As I started making things more readable versus trying to micro-optimize, I found my complaints started disappearing, questions reduced (drastically), and more compliments appeared. It was also noticeable that I was being treated as if I was more professional, because any questions were much easier to explain. This made clients happier, thus resulting in happier superiors, and more work coming my way.


KISS never applies more than in the analytics and tag management field. Keep it simple, keep it readable. Those micro-optimizations are just that - micro - so always opt for the more readable approach. I know I'm not the only one out there that thinks this, so please comment below on your experiences!