New NodeJS 12 feature look more enterprise java now

New NodeJS 12 support thread and it is more stable

Threads are almost stable!

With the last LTS we’ve got access to threads. Of course, it was an experimental feature and required a special flag called –experimental-worker for it to work.

With the upcoming LTS (Node 12) it’s still experimental, but won’t require a flag anymore. We are getting closer to a stable version!

https://morioh.com/p/027e49ee6bce/new-node-js-12-features-will-see-it-disrupt-ai-iot-and-more-surprising-areas

V8 Gets an Upgrade: V8 update to V8 7.4

Hello TLS 1.3

Properly configure default heap limits

Switch default http parser to llhttp

Making Native Modules Easier — progress continues

Worker Threads

Diagnostic Reports

Heap Dumps

Startup Improvements

ES6 Module Support

New compiler and platform minimums

https://medium.com/@nodejs/introducing-node-js-12-76c41a1b3f3f

Phantomjs fails to load HTTPS issue as switching to tlsv1

I had an apps to use phantomjs to generate a report by using Google Charts.

The apps will spawn a child process to execute phantomjs command, to load a web page that contains the charts, and then take a screenshots saving into JPG, which later convert into PDF for archive.

In the code, it needs to load the following JS URL from Google: https://www.google.com/jsapi, which is HTTPS based.

Suddenly it is stopped working since last month.

By checking the log and found that PhantomJS encountered JavaScript error: “ReferenceError, can’t find Google”. Hey, I tried to open the web page in my local Chrome browser, and it still working. I tried to CuRL from my server, still can download the page. So I suspected might be SSL issues on PhantomJS.

Tried googling and found a solution. As Google had already deprecated the old version of SSL (SSLv3) and use TLSv1.

In the command line, we need to add one more option: –ssl-protocol=tlsv1, to bypass the HTTPS error, and it turns out it is working now.


phantomjs --ssl-protocol=tlsv1 your-code.js

JavaScript inet_aton functions comparison

I came across this stun.js code and found one interesting implementation of inet_aton functions. I curious of its formula and equation.

function inet_aton(a) {
    var d = a.split('.');
    return ((((((+d[0])*256)+(+d[1]))*256)+(+d[2]))*256)+(+d[3]);
}

I written a simple Node.JS test script just to evaluate, calculate different implementations of inet_aton, and here is my findings:

function inet_aton(a) {
    var d = a.split('.');
    return ((((((+d[0])*256)+(+d[1]))*256)+(+d[2]))*256)+(+d[3]);
}
function inet_aton_b(ip){
    var a = new Array();
    a = ip.split('.');
    return((a[0] < < 24) >>> 0)  + ((a[1] < < 16) >>> 0) + ((a[2] < < 8) >>> 0) + (a[3] >>> 0);
}

function inet_aton_c(a) {
   var d = a.split('.');
   return d[0] * 256 * 256 * 256 + d[1] * 256 * 256 + d[2] * 256 + d[3];
}

var start;
start = new Date().getTime();
for(var i = 0; i < 1000000; i++) {
inet_aton('1.1.1.1')
}
console.log('elapsed:' + (new Date().getTime() - start));


start = new Date().getTime();
for(var i = 0; i < 1000000; i++) {
inet_aton_b('1.1.1.1')
}
console.log('elapsed:' + (new Date().getTime() - start));

start = new Date().getTime();
for(var i = 0; i < 1000000; i++) {
inet_aton_c('1.1.1.1')
}
console.log('elapsed:' + (new Date().getTime() - start));

See the first implementation run faster. The key point is lesser multiplication is performed, by factoring using simple math.

I run the same code on Java but I found different things, I can't tell which one run faster than other one. If I run the first code first, second code followed, always the second block faster than first block. If I exchange, run second code first, first code followed, always the second block faster than first block. They both run in same speed. I even use javap to view its bytecode and found second code have more instruction than first one, but might be JVM can do its optimization so that end result both code run same speed.

Java/JavaScript Tips on Append to File

This is Java/JavaScript Tips on Append content to file. I will introduce append file via command line, Java, and Node.JS (JavaScript).

To append file via command line, in Windows or Unix, you can do:

echo "test" >> a.txt

The file a.txt will be created if not exist at the first time. And the subsequence calls of >> will append the content to next line of the end of file.

To append file via Java, you can do:

PrintWriter pw = new PrintWriter(new FileWriter(new File("a.txt"), true)); 
pw.println("test");
pw.close();

The file a.txt will be created if not exist at the first time. And the subsequence calls of PrintWriter.println will append the content to next line of the end of file.

The interesting thing is I like to use PrintWriter, to call println, which I not need to append \n myself.

Notice the true variable at new FileWriter, this let Java know you want to append file, if you not provide this variable, Java will always overwrite the file but not appending.

To append file via JavaScript, you can do:

in asynchronous calls:

var fs = require('fs');
fs.appendFile('a.txt', 'content\n', function(err) {
 // some callback after append file
});

OR

in synchronize calls:

var fs = require('fs');
fs.appendFileSync('a.txt', 'content\n');

The file a.txt will be created if not exist at the first time. And the subsequence calls of appendFile or appendFileSync will append the content to next line of the end of file.

Node.JS provides asynchronous and synchronize versions for each File IO Calls. Refer the link here. Other than appendFile, for example, readFile/readFileSync, writeFile/writeFileSync.

Node.JS itself is a single threaded and asynchronous. It is always recommended to use asynchronous version of method instead of synchronize version, as synchronize version is a blocking calls. If the content need append to file is very very big, the calls will just hang there, Node.JS cannot handle other requests anymore. To learn how Node.JS back end works, refer here.

JavaScript Tips on Objects and Array and their For loop

This is JavaScript Tips on Objects and Array and their For loop.

In JavaScript, there is an Object and represented as:


var someObj = {name:"YongHao", age:25}

There is an Array and represented as:


var someArr = [1,2,3,4]

Use normal for loop only for Array, and use for…in only for Object.

For example, for Object

var someObj = {name:"YongHao", age:25}
for(var i in someObj) {
  console.log(i + ' - ' + someObj[i]);
}

We get:
name – YongHao
age – 25

For Array

var someArr = [1,2,3,4]
for(var i = 0; i < someArr.length; i++) {
  console.log(i + ' - ' + someArr[i]);
}

We get:
0 - 1
1 - 2
2 - 3
3 - 4

Do not use for...in to Array, if you do:

var someArr = [1,2,3,4]
for(var i in someArr) {
  console.log(i + ' - ' + someArr[i]);
}

It may return good result, but not recommended, as i now is a string but not an integer.