Console.log behaves differently in node than in {N}


#1

Here’s a simple for loop, where you fill an array with objects. Then you log the objects’ property.

I assume most (or many of you) use VScode, so create a new, blank file, set the type to “JavaScript” and hit run. The output of the code is logged to the console as a simple 5x5 matrix of ones (1), since we have a total of 25 element array. Phew, that was simple.

let _pieces = [];
let _rows = 5;
let _columns = 5;

for (let i = 0; i < _columns * _rows; i++) {
	_pieces[i] = { id: i, foo: 1 };
 }

for (let columns = 0; columns < _columns; columns++) {
	let buffer = ""
	for (let rows = 0; rows < _rows; rows++) {
		let index = columns * _columns + rows;
		buffer += _pieces[index].foo + " ";
	}
	console.log(buffer);
}

Now copy and paste the code into a basic hello world {N} template app and run.

The output of the code will be a single line (5) of ones (1). What happened to the rest!?

Does {N} do some sort of optimization on console.log, so if the value does not change it doesn’t output the rest!? That would be weird. So why am I not getting the 5x5 matrix output in {N}, but I do get it in a simple node console?

(Note, that if you change the value of “foo” in the object to, say “i + 1”, then it logs the 5x5 matrix again. But if the value of “foo” is static (doesn’t change) it only logs the first 5 values in {N}, ignoring the rest.)


#2

Is this iOS or Android? Also, are you sure there isn’t a number to the left of the log, stating how many times the same log has gone through, collapsing the same content into 1 line?

Also, the ADB Logcat (the Android system logs) seems to do some optimizations, and not display all occurrences of similar messages. Since the VSCode uses the NativeScript CLI to parse and display logs, and the NativeScript CLI, in turn, reads the logs from the adb logcat, the Logcat tool is to blame.

Good news is if you are interested in using the console to debug your app, the next version of NativeScript will bring significant ‘console’ improvements. To make sure you are viewing all logs, and that they are not ‘collapsed’, it is best to read the console in Chrome DevTools tns debug android | ios.

tl;dr - ADB logcat optimization; Chrome DevTools debugging for Android will not have the same limitation as its messages are channeled separately.

ADB
ADB

Chrome DevTools


#3

Thanks for the info, Pete. It’s a plain JS {N} project for both platforms. I’m using only the emulators, but that’s irrelevant at this point, since I’m just outputting logs into the console. I’m only using VScode so no, there are no left/right sections in the log console with extra info. Not familiar with ADB and I never touched Android Studio (other than as a required installation). But chrome debugging might be interesting. Will give it a try at some point.

Thanks!