Performance and console.log?


#1

I just noticed something surprising with our nativescript-angular app, that we’re about the go into open beta with.

We couldn’t understand why it performed horrible even on my Sony XPeria Z3, until I tried to once again profile it.

It looks like console.log is either very CPU extensive or blocks the main threat, at least on Android.
A page switch to a simple page would according the profiler spend 2½-3s on console.log and about ½s on everything else, and the navigation transition looked sluggish.
Once I disabled all the console.log statements, it became much smoother and the page switch took little over ½s including the animation time.

My question is, can/should console.log() really cost that much?

EDIT:
I probably should note that there where (only?) about 20 log statements from the various components and services used by the page.


#2

Do you log plain strings, or do you operate over the strings before console.logging? I am more than curious to see what could be causing the slowdown. Under Android, console.log is implemented in JavaScript to format the string if necessary (stringify objects), and android.util.Log is what is called ultimately.

You could try making this drop-in replacement, and report back whether the performance remains bad. This will verify if it’s the core-modules’ implementations that is slowing your app down.

globals.console = {
    log = function(obj) {
        var msg = obj;
        if (typeof obj !== "string" || !(obj instanceof String)) {
            msg = JSON.stringify(obj);
        }

        android.util.Log.v("~~ JS ~~", msg);
    }
}

#3

Hi @Pete.K

I log plain text with console.log(...). The strings are created whether or not they’re written to the console as they run through a LogService

I haven’t tried your suggestion yet, so I don’t know it that would change anything.