• Serve Del Mar

    For my friends in the Bay Area: Tomorrow, August 3rd, there’s a great volunteering opportunity to help Del Mar High School in San Jose get ready for their school year. We plan to help clean up their campus, renovate the teacher’s lounge, host free health check-ups for students, and more. Here’s what our last event was like:

    http://vimeo.com/65408866

    Everyone is welcome! Bring a smile and great attitude. Stop by during 8am-12pm or 11am-3pm on August 3rd. Get directions. We’re also collecting backpacks and school supplies (pens, pencils, highlighters, binders, etc.) for students who need them. If you’re interested in coming or can donate a backpack or supplies, let us know or RSVP on Facebook.

    Also, I’ve started a small tradition of going out for breakfast at our last event. If you’re also a fan of breakfast, stop by Stacks in Campbell bright-and-early at 7am. See you there!

  • Why mobile web apps are slow

    Drew Crawford wrote a very long and well-cited article about why mobile web apps are slow.

    Now I am going to warn you–this is a very freaking long article, weighing in at very nearly 10k words. That is by design. I have recently come out in favor of articles that are good over articles that are popular. This is my attempt at the former, and my attempt to practice what I have previously preached: that we should incentivize good, evidence-based, interesting discussion and discourage writing witty comments.

    The article primarily discusses JavaScript performance. After reading Drew’s article, you’ll understand why JS usage on mobile has to be relatively light compared to what you can get away with on desktop.

    I’d love to see more of this good discussion on non-JavaScript impediments to creating good mobile web apps.

  • NeDB: SQLite for Node

    I’ve been using the Node Embedded Database in a project and its MongoDB-compatible API has been a joy to use. DailyJS has a short and sweet writeup of NeDB and how it works. While my current project has outgrown it, I’m considering using NeDB for Yeti since it’s pure Node and can even be used in browsers.

  • Stream Concat Anti-Pattern

    NodeConf 2013 was great. You should go. I learned a lot of new stuff. Yesterday, I applied things I learned there for the first time.

    I often write code that looks like something like this:

    var stdout = "",
        child = cp.spawn(process.execPath, args);
    
    child.stdout.setEncoding("utf8");
    child.stdout.on("data", function (chunk) {
        stdout += chunk;
    });
    child.on("close", function () {
        cb(null, stdout);
    });
    

    I want to buffer stdout data that comes from the child process until it exits. Typically, I do this by appending data as it comes in (chunk) to a string variable (stdout).

    This is an anti-pattern. There’s no need for the data I’m accumulating in stdout to be a string. When I convert Buffers into strings with setEncoding("utf8"), and then append them to create an even bigger string, I’m needlessly copying data into V8 when it’s more efficient to keep it outside.

    We want to keep as much data as we can inside of Buffers, which corresponds to memory outside of the V8 heap. Node makes this easy. Since I’m collecting a lot of data here, I could do this better by rewriting that code to the following:

    var stdout = [],
        child = cp.spawn(process.execPath, args);
    
    child.stdout.on("data", function (chunk) {
        stdout.push(chunk);
    });
    child.on("close", function () {
        if (Buffer.isBuffer(stdout[0])) {
            cb(null, Buffer.concat(stdout));
            return;
         }
         cb(null);
    });
    

    Instead of taking large strings and appending them to enlarge another larger string, I can simply gather all of the Buffer chunks in an array until I’m ready to concat them all into a single large Buffer.

    If I needed to read part of the output, perhaps the last few characters, I can use the Buffer’s slice method to convert a small part of the total results to a string for inspection.

    A smart way to do this easily is to use the concat-stream module, which does this all for you.

    If you liked this, you’ll love going on substack’s stream-adventure. You’re guaranteed to learn something, so give it a try.

  • Do For One

    Sometimes I cruise around on my Tikit. Yesterday I didn’t fare too well.

    San Thomas El Camino Intersection

    While making a left turn, my tiny 16″ wheel sank into the ground at 15 mph. The pothole blended in nicely with years of oil stains from years of cars waiting to turn left. I couldn’t keep the bike under control with one hand signaling left. Next thing I knew, I was on the ground.

    I banged up my knee and had some scrapes. Nothing too bad. But what surprised me was that the driver of the car next to me immediately got out and helped me off the ground. He insisted on giving me a bottled water. He made sure I was okay before taking off. Another woman who saw me limping away stopped her car to give me bandages. I was stoked to see these strangers help me out. But as an introverted guy, inside my car driving by, would I do the same?

    Cars, phones, earbuds, the iPad I’m typing on: All of these things can be world-canceling devices. That isn’t always bad, of course. But it’s easier to miss the opportunity for better use of our time. Jonathan Safran Foer, in his NYT article How Not to Be Alone, shared his experience using a phone to avoid a difficult moment with another person:

    The phone didn’t make me avoid the human connection, but it did make ignoring her easier in that moment, and more likely, by comfortably encouraging me to forget my choice to do so. My daily use of technological communication has been shaping me into someone more likely to forget others. The flow of water carves rock, a little bit at a time. And our personhood is carved, too, by the flow of our habits.

    …everyone is always in need of something that another person can give, be it undivided attention, a kind word or deep empathy. There is no better use of a life than to be attentive to such needs. There are as many ways to do this as there are kinds of loneliness, but all of them require attentiveness, all of them require the hard work of emotional computation and corporeal compassion.

    While we must balance the time we spend on others with time we spend on ourselves, the best parts of my life are those spent helping others. I find myself leveraging modern tools to ignore others, almost by habit, missing the best use of my life.

    The nice strangers who took time out of their day to help me, garnering ire from the traffic behind them, are inspirations to a life-long introvert like me who would struggle to decide to open my car door.

    We’re carved by our habits. Our big decisions are made by all of the little choices we make. Nobody will ever see most of these choices, but that’s okay. Deciding to engage with a messy world in small things will carve out an attitude of compassion that can make a big difference over time.

    You can’t help everyone, but why not do for one what you wish you could do for all?