eph baum dot dev

← Back to blog

Array.prototype.diff(meaCulpa)

Published on 08/23/2014 08:45 PM by Eph Baum

So, it turns out that when you go off all half-cocked all willy-nilly extending a core functionality of JavaScript it’s bound to result in a hiccup now and then.

It was, then, a few days after posting about my new ‘diff’ function that I found my JavaScript choking on exactly that function. It turns out that the issue was that I wasn’t watching my data types carefully enough.

The solution? Simply watch what you’re doing!

Array.prototype.diff = function( a ) { 
  if ( a instanceof Array && this instanceof Array) { 
    return this.filter( function( i ) { 
      return a.indexOf( i ) < 0; 
    }); 
  } else { 
    return new Array(); 
  } 
};

At this point we’re doing the EXACT same thing that we had been doing before except prior to comparing the two arrays, we check to make sure that they are both actually arrays using instanceof, as you can see above.

You’ll see that the else now simply returns an empty array if they’re not both arrays. This solution works for me but you could definitely do quite a bit more with this if you felt it was warranted.

Written by Eph Baum

  • AI

    AI

    This may read as alarmist. It might not be alarmist enough. On AI as sophisticated autocomplete, not thinking—and why that gap matters to the future.

  • Slow Down

    Slow Down

    Speed worship is anxiety wearing a productivity costume. The case for slowing down—and why going fast is often how you screw everything up.

  • Making Brutalist Design Accessible: A Journey in WCAG AA Compliance

    Making Brutalist Design Accessible: A Journey in WCAG AA Compliance

    How I transformed my brutalist blog theme to meet WCAG AA accessibility standards while preserving its vibrant, random aesthetic. Talking about contrast ratios, color theory, and inclusive design.

  • Building Horror Movie Season: A Journey in AI-Augmented Development

    Building Horror Movie Season: A Journey in AI-Augmented Development

    How I built a production web app primarily through 'vibe coding' with Claude, and what it taught me about the future of software development. A deep dive into AI-augmented development, the Horror Movie Season app, and reflections on the evolving role of engineers in the age of LLMs.

  • Chaos Engineering: Building Resiliency in Ourselves and Our Systems

    Chaos Engineering: Building Resiliency in Ourselves and Our Systems

    Chaos Engineering isn't just about breaking systems — it's about building resilient teams, processes, and cultures. Learn how deliberate practice strengthens both technical and human architecture, and discover "Eph's Law": If a single engineer can bring down production, the failure isn't theirs — it's the process.

  • Using LLMs to Audit and Clean Up Your Codebase: A Real-World Example

    Using LLMs to Audit and Clean Up Your Codebase: A Real-World Example

    How I used an LLM to systematically audit and remove 228 unused image files from my legacy dev blog repository, saving hours of manual work and demonstrating the practical value of AI-assisted development.