Eric 发布的文章

<exploring ES6> 读书笔记

About ECMAScript 6

  1. ECMA = European Computer Manufacturers Association, 是一个致力于信息和通讯标准化的组织. 更多信息

  2. ES6 = ECMAScript 6 是一个脚本语言规范. 最早是用来规范JavaScript的, 现如今JavaScript也是这个规范最好的实现.其它实现比如 JScript 和 ActionScript. 更多信息,比如ECMAScript 2015, ECMAScript 2016, ECMAScript 2017, ECMAScript 2018, 参见 wiki

  3. ES6 compatibility table

  4. ECMAScript 由TC39 (Ecma Technical Committee 39)委员会制定,它主要由大的浏览器厂商组成;

  5. ES6 亦 ECMAScript 2015 是多年后憋的一个大的release, 之后的ECMAScript 2016,等每年都会发布一些新的小更新;

  6. JavaScript是Oracle(从Sun继承而来)的一个商标,所以 JavaScript的官方名称是ECMAScript.

  7. 围绕 JavaScript 有: 1) JavaScript规范,即 ECMAScript 2015,2016,...; 2)JavaScript的Engine, 即规范实现者, 如V8; 3)JavaScript的使用者, 即应用开发人员;

  8. ES6是ES5的超集, 没有移除任何feature. 为什么? 难以控制的升级, 老旧代码,世界各地的各种不同的浏览器

  9. ES6的主要的features分类
    a. 更好的语法 如: Class, Module;
    b. 标准库的新功能, 如: Strings, Arrays, Promise, Map, Set;
    c. 新feature, 如: Generator, Proxy, WeakMap;

  10. Strict mode was introduced in ECMAScript 5 to clean up the language.

    'use strict';

The bodies of modules and classes are implicitly in strict mode in ECMAScript 6 – there is no need for the 'use strict' marker. Given that virtually all of our code will live in modules in the future, ECMAScript 6 effectively upgrades the whole language to strict mode.

关于 Node.js 的setImmediate, setTimeout, process.nextTick

要根据 Node.js 的event loop 来理解这些 setImmediate, setTimeout, process.nextTick

Node.js actually provides its own implementation of these methods. Timers integrate very closely with the system, and despite the fact that the API mirrors the browser API, there are some differences in implementation.

console.log("start of hello");

setImmediate(()=>{
    console.log("in setImmediate");
    setImmediate(()=>{
        console.log("in setImmediate's setImmediate"); //the 3rd loop
    });
});

setTimeout(()=>{
    console.log("in setTimeout");
    setImmediate(()=>{
        console.log("in setTimeout's setImmediate"); //2nd loop 
    });
}, 0);

process.nextTick(()=>{
    console.log("in nextTick"); //next event action
    process.nextTick(()=>{
        console.log("in nextTick's nextTick"); // next event action
    });
});

console.log("end of hello");

process.nextTick 是当前event action 处理完之后, 下一个会处理的action.

Node.js 文档

Node.js 使用event loop的并发模型 vs 其它语言的 多线程并发模型;
Node.js 的关键是 对于所有的非CPU 计算操作全部都使用异步的callback 模式;

基本语法

node [options] [V8 options] [script.js | -e "script" | -] [--] [arguments]
node inspect [script.js | -e "script" | <host>:<port>] …
node --v8-options

Node.js 对于 ES2015 Support
https://node.green/

Node.js 文档: https://nodejs.org/en/docs/

启用debug:
node --inspect hello.js
会打出如下:

chrome-devtools://devtools/remote/serve_file/@60cd6e859b9f557d2312f5bf532f6aec5f284980/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/6285270a-4795-40d7-992f-cfc452461144

然后打开9229端口:

http://localhost:9229/json/list

即便一开始没有启用 --inspect 参数, 启动之后, 还是可以通过发送 SIGUSR1 signal, 让它打开debuger (kill -l 查看全部signal)

Node.js is an event-based platform. This means that everything that happens in Node is the reaction to an event. A transaction passing through Node traverses a cascade of callbacks.

关于 Event Loop: link
https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop

SRE - performance

General

  1. warm up requests;
  2. zip the big request/response for API;

Java

  1. Full GC after server startup;
  2. Thread pool/Connection pool;

关于 java 的 finalize() 方法,Finalizer类, Finalizer 线程, 以及这些对象的GC

  1. 若一个类实现Object类的 finalize() 方法, 当去实例化这个类当时候,JVM会自动创建一个Finalier实例;
  2. 这个Finalizer实例会refer 这个带有finalize的实例,并且这个Finalizer类会自动被加到一个全是Finalizer的链表结构中;
  3. Finalizer 类有个static的ReferenceQueue的队列, 这个队列里面存的是上面提到的Finalizer. 当且仅当没有其它对象引用这个带有finalize()方法的对象, 仅剩一个Finalizer引用它的时候, 它对应的这个Finalizer 就会被GC 线程加入这个queue.
  4. 还有一个Finalizer 线程, 它会从上面提到的这个block ReferenceQueue中拿Finalizer, 然后去执行它对应实例的finalize() 方法, 然后从队列中去除它;
  5. Finalizer 线程相对于其它线程, 优先级比较低, 所以有竞争时分配的时间比较少;
  6. 更多实例 参看: https://plumbr.io/blog/garbage-collection/debugging-to-understand-finalizer