• Working in the Shed

    Fellow procrastinator Matt Gemmell is facing something very relatable: the constant availability of time-sucking distractions and what we can do about it.

    We have limited time. Our workdays are only so long. Our evenings. Our lives. We spend too much of our time on trivia. Some distraction is healthy and necessary, but we all know that the scales have long since tipped.

    The internet isn’t to blame – it’s us. We’re weak, and our natural tendency is to feed that weakness rather than struggle against it. Some people are more prolific than others, but the boundaries don’t lie where we think they do: context and self-discipline are much, much more important than your personal pace or ability. The difference that a creativity-conducive environment can make is profound.

  • 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.