ADM Blog

JSProfiler – Javascript memory profiler

JsProfiler is a small javascript that I've build to help me identify memory leaks and improve overall performance of some code I was working on.
It works by wrapping all the methods in the given context and keeping track of the number of times they run, how much memory they consume and how long they take to execute.

The results can be viewed real time in a small overlay or you can track specific methods and see the output in the console.
Unfortunately it only works in Google Chrome since Chrome exposes the memory information.

More usage information and download on github


Make a JavaScript getter act both as getter and as a function

So I have a object that exposes a getter to retrieve data after some expensive synchronous computation.

    function processData() {
       // lots of stuff happening here
       return result;
   return {
      get data () {
          // some checks happening here
           return procesData();
var obj = new MyObject();
var x =; // this stops things for 2 or 3 seconds
// .. carry on

Since this is part of an already used library, is getting pretty hard to change the public interface for backwards compatibility reasons.
The thing is I've added a asynchronous processData method and i want to make that available as well without changing the public methods. The normal thing to do would be to convert the getter into a method called getData() and getDataAsync(callback) but that wouldn't work because it will annoy some people when they see their code isn't working anymore.

The solution couldn't be easier but it took me a while to figure it out and as always, it's sharing time.

    function processData() {
       // lots of stuff happening here
       return result;
    function processDataAsync(callback) {
      if (!arguments.length || arguments.length && typeof arguments[0] !== "function") {
            return processData();
      // lots of stuff happening here but asynchronously 
    processDataAsync.__proto__ = {valueOf : processData};
   return {
      get data () {
           // some checks happening here
           return processDataAsync;
var obj = new MyObject();
var x =; // get data synchronously
// .. carry on after 2 seconds
var obj = new MyObject();
var x =; // still a synchronous call since no callback was given
// .. carry on after 2 seconds
var obj = new MyObject(); {
    // get the data here after few seconds without blocking anything
// .. carry on right away

So now the same getter can be used from now on as a asynchronous function if used with parentheses and given a callback or as a simple getter.


Pixel Programming

If you're old enough you may have seen the times when programmers wrote code as optimal as possible given the computers available back then. I remember 3D shooter games that would average 300 - 400 KB in size and you would play countless levels for days. Here's for example a 96 KB 3D shooter game called .kkriger that will leave you breathless.

If you try now to explain people these days that it takes less space to write a 3 minutes HD animated video and music clip than than it takes to store a 32×32 pixel icon they won't believe you. Not now when a plain jpeg image used on some website is > 50KB. So here's a guy called Iñigo Quílez who demonstrates just that using the image analogy. He writes a x86 real mode demo using Photoshop. The entire program is done by creating a 9x9 pixels image, filling each pixels with carefully chosen colors to represent the correct opcodes (who needs compilers, right ?) The image is then saved in a .raw format (no headers) then renames it to a .com file so that Windows recognize it as a executable. The animation is a infinite moving tunnel with a gray colored texture created with an XOR pattern, blurred, and deformed using a plane deformation technique to produce the animation.

This is the "source code" resized 10 times in a png extension

And here's the guy in action proving his mad skills.


Read data directly from base64 without decoding

So here's the catch. You have a binary input you want to mess with in your browser for whatever reason. First problem you encounter is when you load the file. In IE you will have trouble reading a binary string because it uses null terminated strings, and your file will end at the first 0x00 byte found. If you manage to overcome that but trust me it is very CPU intensive. See the source of Read/Load files from ZIP in Javascript post. You will have to create a vbScript that will convert the received AJAX response into an array with decimal values for each byte in the file. Depending on your file size, it will take accordingly (10+ seconds * file KB). For every other browser it works just fine with plain strings.

Okay, one other potential issue may come if you need to receive the data into a JSON string or XML or whatever beside a plain binary file. Then you have to use BASE64 encoding and decode your data into a variable but must keep in mind the first issue with the null characters. My solution to this problem is to create a method to read directly from the BASE64 encoded string at arbitrary locations without decoding the entire thing.

Here's an example of a binary file:

Offset(h)00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00000000 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F     ...............
00000010 19 84 74 68 69 73 20 69 73 20 61 20 74 65 73 74     ..this is a test

You have 32 bytes in this file, with BASE64 encoding you get a 46 chars length string:


A usage example:

    var data = new Base64Native("AAECAwQFBgcICQoLDA0ODxmEdGhpcyBpcyBhIHRlc3Q=");
    console.log(data.length); // outputs 32;
    console.log(data.readBoolean()); // outputs false; - reads a byte and casts it to a boolean value
    console.log(data.readByte()); // outputs 1;
    console.log(data.readWord()); // outputs 515; - reads a 16 bit number
    console.log(data.readInt()); // outputs 67438087; - reads a 32 bit number
    console.log(; // moves the pointer to byte 28 (0x1C)
    console.log(data.readString(data.bytesAvailable())); // outputs 'test'; reads a string from position 28 to the end of the file

And here's the class

var Base64Native = function(data) {
    var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
        length = data.length - (data.length / 4) - (data.split(/=/g).length - 1),
        ptr = 0;
    function keyIdx(idx) {
        return keyStr.indexOf(data.charAt(idx));
    function readByte() {
        if (!(length - ptr)) { throw Error('End of stream!'); }
        var blockIndex = Math.floor(ptr / 3) * 4, subIndex = ptr++ % 3, ptrChr = blockIndex + subIndex;
        return !subIndex && ((keyIdx(ptrChr) < < 2) | (keyIdx(ptrChr + 1) >> 4)) || subIndex === 1 &&
               (((keyIdx(ptrChr) & 15) < < 4) | (keyIdx(ptrChr + 1) >> 2)) || ((keyIdx(ptrChr) & 3)  < < 6) |  keyIdx(ptrChr + 1);
    return {
        /* Returns the available bytes left in the file to read */
        bytesAvailable : function() {
            return length - ptr;
        /* Reads and returns a 8 bit number and increments the position of the index accordingly */
        readByte : function() {
            return readByte();
        /* Reads and returns a 32 bit number and increments the position of the index accordingly */
        readInt : function() {
            return (readByte() << 24) | (readByte() << 16) | (readByte() << 8) | readByte();
        /* Reads 1 byte and returns the boolean value of it and increments the position of the index accordingly */
        readBoolean : function() {
            return Boolean(readByte());
        /* Reads and returns a 16 bit number and increments the position of the index accordingly */
        readWord : function() {
            return readByte() << 8 | readByte()
        /* Reads and returns a string of length 'len' and increments the position of the index accordingly */
        readString : function(/*String*/len) {
            var data = '';
            for (var i = 0; i < len; i++) {
                data += String.fromCharCode(readByte());
            return data;
        /* Moves the index into the file at location newpos */
        seek : function(newpos) {
            ptr = newpos;
        /* Returns the position of the index in the file */
        position : function() {
            return ptr;
        /* The length of the data */
        length : length

This is water

Adapted from a commencement speech given by David Foster Wallace to the 2005 graduating class at Kenyon College. Mr. Wallace, 46, died last Friday, after apparently committing suicide

There are these two young fish swimming along, and they happen to meet an older fish swimming the other way, who nods at them and says, "Morning, boys, how's the water?" And the two young fish swim on for a bit, and then eventually one of them looks over at the other and goes, "What the hell is water?"

If at this moment, you're worried that I plan to present myself here as the wise old fish explaining what water is to you younger fish, please don't be. I am not the wise old fish. The immediate point of the fish story is that the most obvious, ubiquitous, important realities are often the ones that are the hardest to see and talk about. Stated as an English sentence, of course, this is just a banal platitude -- but the fact is that, in the day-to-day trenches of adult existence, banal platitudes can have life-or-death importance. That may sound like hyperbole, or abstract nonsense.

A huge percentage of the stuff that I tend to be automatically certain of is, it turns out, totally wrong and deluded. Here's one example of the utter wrongness of something I tend to be automatically sure of: Everything in my own immediate experience supports my deep belief that I am the absolute center of the universe, the realest, most vivid and important person in existence. We rarely talk about this sort of natural, basic self-centeredness, because it's so socially repulsive, but it's pretty much the same for all of us, deep down. It is our default-setting, hard-wired into our boards at birth. Think about it: There is no experience you've had that you were not at the absolute center of. The world as you experience it is right there in front of you, or behind you, to the left or right of you, on your TV, or your monitor, or whatever. Other people's thoughts and feelings have to be communicated to you somehow, but your own are so immediate, urgent, real -- you get the idea. But please don't worry that I'm getting ready to preach to you about compassion or other-directedness or the so-called "virtues." This is not a matter of virtue -- it's a matter of my choosing to do the work of somehow altering or getting free of my natural, hard-wired default-setting, which is to be deeply and literally self-centered, and to see and interpret everything through this lens of self.

People who can adjust their natural default-setting this way are often described as being "well adjusted," which I suggest to you is not an accidental term.

Given the triumphal academic setting here, an obvious question is how much of this work of adjusting our default-setting involves actual knowledge or intellect. This question gets tricky. Probably the most dangerous thing about college education, at least in my own case, is that it enables my tendency to over-intellectualize stuff, to get lost in abstract arguments inside my head instead of simply paying attention to what's going on right in front of me. Paying attention to what's going on inside me. As I'm sure you guys know by now, it is extremely difficult to stay alert and attentive instead of getting hypnotized by the constant monologue inside your own head. Twenty years after my own graduation, I have come gradually to understand that the liberal-arts cliché about "teaching you how to think" is actually shorthand for a much deeper, more serious idea: "Learning how to think" really means learning how to exercise some control over how and what you think. It means being conscious and aware enough to choose what you pay attention to and to choose how you construct meaning from experience. Because if you cannot exercise this kind of choice in adult life, you will be totally hosed. Think of the old cliché about "the mind being an excellent servant but a terrible master." This, like many clichés, so lame and unexciting on the surface, actually expresses a great and terrible truth. It is not the least bit coincidental that adults who commit suicide with firearms almost always shoot themselves in the head. And the truth is that most of these suicides are actually dead long before they pull the trigger. And I submit that this is what the real, no-bull- value of your liberal-arts education is supposed to be about: How to keep from going through your comfortable, prosperous, respectable adult life dead, unconscious, a slave to your head and to your natural default-setting of being uniquely, completely, imperially alone, day in and day out.

That may sound like hyperbole, or abstract nonsense. So let's get concrete. The plain fact is that you graduating seniors do not yet have any clue what "day in, day out" really means. There happen to be whole large parts of adult American life that nobody talks about in commencement speeches. One such part involves boredom, routine, and petty frustration. The parents and older folks here will know all too well what I'm talking about.

By way of example, let's say it's an average day, and you get up in the morning, go to your challenging job, and you work hard for nine or ten hours, and at the end of the day you're tired, and you're stressed out, and all you want is to go home and have a good supper and maybe unwind for a couple of hours and then hit the rack early because you have to get up the next day and do it all again. But then you remember there's no food at home -- you haven't had time to shop this week, because of your challenging job -- and so now after work you have to get in your car and drive to the supermarket. It's the end of the workday, and the traffic's very bad, so getting to the store takes way longer than it should, and when you finally get there the supermarket is very crowded, because of course it's the time of day when all the other people with jobs also try to squeeze in some grocery shopping, and the store's hideously, fluorescently lit, and infused with soul-killing Muzak or corporate pop, and it's pretty much the last place you want to be, but you can't just get in and quickly out: You have to wander all over the huge, overlit store's crowded aisles to find the stuff you want, and you have to maneuver your junky cart through all these other tired, hurried people with carts, and of course there are also the glacially slow old people and the spacey people and the ADHD kids who all block the aisle and you have to grit your teeth and try to be polite as you ask them to let you by, and eventually, finally, you get all your supper supplies, except now it turns out there aren't enough checkout lanes open even though it's the end-of-the-day-rush, so the checkout line is incredibly long, which is stupid and infuriating, but you can't take your fury out on the frantic lady working the register.

Anyway, you finally get to the checkout line's front, and pay for your food, and wait to get your check or card authenticated by a machine, and then get told to "Have a nice day" in a voice that is the absolute voice of death, and then you have to take your creepy flimsy plastic bags of groceries in your cart through the crowded, bumpy, littery parking lot, and try to load the bags in your car in such a way that everything doesn't fall out of the bags and roll around in the trunk on the way home, and then you have to drive all the way home through slow, heavy, SUV-intensive rush-hour traffic, etcetera, etcetera.

The point is that petty, frustrating crap like this is exactly where the work of choosing comes in. Because the traffic jams and crowded aisles and long checkout lines give me time to think, and if I don't make a conscious decision about how to think and what to pay attention to, I'm going to be pissed and miserable every time I have to food-shop, because my natural default-setting is the certainty that situations like this are really all about me, about my hungriness and my fatigue and my desire to just get home, and it's going to seem, for all the world, like everybody else is just in my way, and who are all these people in my way? And look at how repulsive most of them are and how stupid and cow-like and dead-eyed and nonhuman they seem here in the checkout line, or at how annoying and rude it is that people are talking loudly on cell phones in the middle of the line, and look at how deeply unfair this is: I've worked really hard all day and I'm starved and tired and I can't even get home to eat and unwind because of all these stupid g-d- people.

Or, of course, if I'm in a more socially conscious form of my default-setting, I can spend time in the end-of-the-day traffic jam being angry and disgusted at all the huge, stupid, lane-blocking SUV's and Hummers and V-12 pickup trucks burning their wasteful, selfish, forty-gallon tanks of gas, and I can dwell on the fact that the patriotic or religious bumper stickers always seem to be on the biggest, most disgustingly selfish vehicles driven by the ugliest, most inconsiderate and aggressive drivers, who are usually talking on cell phones as they cut people off in order to get just twenty stupid feet ahead in a traffic jam, and I can think about how our children's children will despise us for wasting all the future's fuel and probably screwing up the climate, and how spoiled and stupid and disgusting we all are, and how it all just sucks, and so on and so forth...

Look, if I choose to think this way, fine, lots of us do -- except that thinking this way tends to be so easy and automatic it doesn't have to be a choice. Thinking this way is my natural default-setting. It's the automatic, unconscious way that I experience the boring, frustrating, crowded parts of adult life when I'm operating on the automatic, unconscious belief that I am the center of the world and that my immediate needs and feelings are what should determine the world's priorities. The thing is that there are obviously different ways to think about these kinds of situations. In this traffic, all these vehicles stuck and idling in my way: It's not impossible that some of these people in SUV's have been in horrible auto accidents in the past and now find driving so traumatic that their therapist has all but ordered them to get a huge, heavy SUV so they can feel safe enough to drive; or that the Hummer that just cut me off is maybe being driven by a father whose little child is hurt or sick in the seat next to him, and he's trying to rush to the hospital, and he's in a way bigger, more legitimate hurry than I am -- it is actually I who am in his way. Or I can choose to force myself to consider the likelihood that everyone else in the supermarket's checkout line is just as bored and frustrated as I am, and that some of these people probably have much harder, more tedious or painful lives than I do, overall.

Again, please don't think that I'm giving you moral advice, or that I'm saying you're "supposed to" think this way, or that anyone expects you to just automatically do it, because it's hard, it takes will and mental effort, and if you're like me, some days you won't be able to do it, or you just flat-out won't want to. But most days, if you're aware enough to give yourself a choice, you can choose to look differently at this fat, dead-eyed, over-made-lady who just screamed at her little child in the checkout line -- maybe she's not usually like this; maybe she's been up three straight nights holding the hand of her husband who's dying of bone cancer, or maybe this very lady is the low-wage clerk at the Motor Vehicles Dept. who just yesterday helped your spouse resolve a nightmarish red-tape problem through some small act of bureaucratic kindness. Of course, none of this is likely, but it's also not impossible -- it just depends on what you want to consider. If you're automatically sure that you know what reality is and who and what is really important -- if you want to operate on your default-setting -- then you, like me, will not consider possibilities that aren't pointless and annoying. But if you've really learned how to think, how to pay attention, then you will know you have other options. It will actually be within your power to experience a crowded, loud, slow, consumer-hell-type situation as not only meaningful but sacred, on fire with the same force that lit the stars -- compassion, love, the sub-surface unity of all things. Not that that mystical stuff's necessarily true: The only thing that's capital-T True is that you get to decide how you're going to try to see it. You get to consciously decide what has meaning and what doesn't. You get to decide what to worship...

Because here's something else that's true. In the day-to-day trenches of adult life, there is actually no such thing as atheism. There is no such thing as not worshipping. Everybody worships. The only choice we get is what to worship. And an outstanding reason for choosing some sort of God or spiritual-type thing to worship -- be it J.C. or Allah, be it Yahweh or the Wiccan mother-goddess or the Four Noble Truths or some infrangible set of ethical principles -- is that pretty much anything else you worship will eat you alive. If you worship money and things -- if they are where you tap real meaning in life -- then you will never have enough. Never feel you have enough. It's the truth. Worship your own body and beauty and sexual allure and you will always feel ugly, and when time and age start showing, you will die a million deaths before they finally plant you. On one level, we all know this stuff already -- it's been codified as myths, proverbs, clichés, bromides, epigrams, parables: the skeleton of every great story. The trick is keeping the truth up-front in daily consciousness. Worship power -- you will feel weak and afraid, and you will need ever more power over others to keep the fear at bay. Worship your intellect, being seen as smart -- you will end up feeling stupid, a fraud, always on the verge of being found out. And so on.

Look, the insidious thing about these forms of worship is not that they're evil or sinful; it is that they are unconscious. They are default-settings. They're the kind of worship you just gradually slip into, day after day, getting more and more selective about what you see and how you measure value without ever being fully aware that that's what you're doing. And the world will not discourage you from operating on your default-settings, because the world of men and money and power hums along quite nicely on the fuel of fear and contempt and frustration and craving and the worship of self. Our own present culture has harnessed these forces in ways that have yielded extraordinary wealth and comfort and personal freedom. The freedom to be lords of our own tiny skull-sized kingdoms, alone at the center of all creation. This kind of freedom has much to recommend it. But of course there are all different kinds of freedom, and the kind that is most precious you will not hear much talked about in the great outside world of winning and achieving and displaying. The really important kind of freedom involves attention, and awareness, and discipline, and effort, and being able truly to care about other people and to sacrifice for them, over and over, in myriad petty little unsexy ways, every day. That is real freedom. The alternative is unconsciousness, the default-setting, the "rat race" -- the constant gnawing sense of having had and lost some infinite thing.

I know that this stuff probably doesn't sound fun and breezy or grandly inspirational. What it is, so far as I can see, is the truth with a whole lot of rhetorical bullshit pared away. Obviously, you can think of it whatever you wish. But please don't dismiss it as some finger-wagging Dr. Laura sermon. None of this is about morality, or religion, or dogma, or big fancy questions of life after death. The capital-T Truth is about life before death. It is about making it to 30, or maybe 50, without wanting to shoot yourself in the head. It is about simple awareness -- awareness of what is so real and essential, so hidden in plain sight all around us, that we have to keep reminding ourselves, over and over: "This is water, this is water."

David Foster Wallace on Life and Work