Bill' Notes

  • 首页

  • 关于

  • 标签

  • projects

  • 归档

  • 搜索

Event Loop 问答

发表于 2019-04-23 | 更新于 2019-04-24
本文字数: 202 | 阅读时长 ≈ 1 分钟

问答

  • 像 V8 这样的 JavaScript 引擎,除了 call stack 和 heap,是否还有一个 callback queue(message queue/task queue)?
    答:JavaScript 引擎只有一个 call stack 和 一个 heap,callback queue 是由 JavaScript runtime 实现的,比如 Chrome 浏览器或者 NodeJS。
    阅读全文 »

Inside the V8 engine + 5 tips on how to write optimized code

发表于 2019-04-19 | 更新于 2019-04-24
本文字数: 15k | 阅读时长 ≈ 27 分钟

接上一篇关于 JavaScript 的引擎、运行时、调用栈的技术文章,这篇文章主要集中于 V8 引擎以及写出高性能代码的 5 个小建议。
原文链接在这里
此外,这里是一篇中文翻译。

The first post of the series focused on providing an overview of the engine, the runtime and the call stack. This second post will be diving into the internal parts of Google’s V8 JavaScript engine. We’ll also provide a few quick tips on how to write better JavaScript code.

Overview

A JavaScript engine is a program or an interpreter which executes JavaScript code. A JavaScript engine can be implemented as a standard interpreter, or just-in-time compiler that compiles JavaScript to bytecode in some form.

This is a list of popular projects that are implementing a JavaScript engine:

  • V8 — open source, developed by Google, written in C++
  • Rhino) — managed by the Mozilla Foundation, open source, developed entirely in Java
  • SpiderMonkey — the first JavaScript engine, which back in the days powered Netscape Navigator, and today powers Firefox
  • JavaScriptCore — open source, marketed as Nitro and developed by Apple for Safari
  • KJS) — KDE’s engine originally developed by Harri Porten for the KDE project’s Konqueror web browser
  • Chakra(JScript9))  — Internet Explorer
  • Chakra(JavaScript))  — Microsoft Edge
  • Nashorn) - open source as part of OpenJDK, written by Oracle Java Languages and Tool Group
  • JerryScript — is a lightweight engine for the Internet of Things.

Why was the V8 Engine created?
The V8 Engine which is built by Google is open source and written in C++. This engine is used inside Google Chrome. Unlike the rest of the engines, however, V8 is also used for the popular Node.js runtime.

阅读全文 »

an overview of the engine, the runtime, and the call stack

发表于 2019-04-19 | 更新于 2019-06-05
本文字数: 4.1k | 阅读时长 ≈ 7 分钟

转载一篇国外开发者写的技术文章,关于 JavaScript 的引擎、运行时、调用栈。

Overview

Almost everyone has already heard of the V8 Engine as a concept, and most people know that JavaScript is single-threaded or that it is using a callback queue.

In this post, we’ll go through all these concepts in detail and explain how JavaScript actually runs. By knowing these details, you’ll be able to write better, non-blocking apps that are properly leveraging the provided APIs.

If you’re relatively new to JavaScript, this blog post will help you understand why JavaScript is so “weird” compared to other languages.

And if you’re an experienced JavaScript developer, hopefully, it will give you some fresh insights on how the JavaScript Runtime you’re using every day actually works.

The JavaScript Engine

A popular example of a JavaScript Engine is Google’s V8 engine. The V8 engine is used inside Chrome and Node.js for example. Here is a very simplified view of what it looks like:

The Engine consists of two main components:

  • Memory Heap — this is where the memory allocation happens
  • Call Stack — this is where your stack frames are as your code executes
阅读全文 »

浏览器中的 Event Loop

发表于 2019-04-16 | 更新于 2019-04-29
本文字数: 9.5k | 阅读时长 ≈ 17 分钟

Event loop 是针对运行时而非引擎

首先,我们要弄清楚一点,event loop 是针对 JavaScript runtime environment 的,而非 JavaScript engine。对于 NodeJS 这一 runtime environment 来说,它的 JavaScript engine 是 V8,对于 Chrome 这一 runtime environment 来说,它的 JavaScript engine 是 V8 ;对 Firefox 这一 runtime environment 来说,它的 JavaScript engine 是 SpiderMonkey。event loop 是浏览器和 NodeJS 这两个 JavaScript runtime environment 处理异步的机制,它不属于 JavaScript 引擎的运行机制。另外,浏览器和 NodeJS 的 event loop 是不一样的,这篇文章将集中讨论浏览器这一运行时环境。另外要说明的是,浏览器除了包含 JavaScript runtime environment 之外,还负责 HTML 和 CSS 的解析以及最后渲染图形在屏幕上等工作。

JavaScript Engine

负责解析和执行 JavaScript 代码。最开始的时候,JavaScript 的 engine 只是一个解释器,后来以 V8 为代表的现代引擎实现了一种叫做 JIT(just-in-time) 的及时编译技术,拥有更好的性能。

引擎由以下两个主要部分组成:

  • Memory Heap 堆内存,负责内存分配。
  • Call Stack 调用栈,主要用于记录函数调用的位置。

下图是 Chrome 内的 V8 引擎示意图:

JavaScript runtime Environment

JavaScript runtime environment 的示意图如下(以 Chrome 为例):

  • JavaScript Engine - JavaScript 引擎。
  • Web APIs - 由浏览器提供的 APIs,主要包括 DOM、BOM、AJAX、定时器。
  • Callback Queue - 回调队列。也叫作 task queue 或者 message queue。

event loop 是 JavaScript runtime 实现异步的一种机制,负责协调调度以上三者。它的工作是不停地查看调用栈和回调队列,一旦调用栈为空,就通知回调队列将下一个回调函数发送到调用栈,等引擎执行完回调函数,它再进行下一轮循环。

阅读全文 »

ECMAScript 规范阅读笔记之全局对象

发表于 2019-04-16 | 更新于 2019-06-05
本文字数: 752 | 阅读时长 ≈ 1 分钟

ECMAScript 规范中全局对象的阅读笔记。

根据 ECMAScript 的规范,全局对象:

  • 在代码控制流进入任何执行上下文之前,它会被创建。
  • 它没有构造器 [[Constructor]] 这个内部方法;所以不能用 new 操作符创建全局对象的实例。
  • 它没有调用 [[Call]] 这个内部方法;所以不能作为函数调用。
  • 它有一个 [[Prototype]] 内部插槽,其值与各种实现有关。
  • 除了本规范定义的属性,可能还有基于宿主环境定义的属性。
阅读全文 »

前端学习资源索引

发表于 2019-04-16 | 更新于 2019-04-28
本文字数: 353 | 阅读时长 ≈ 1 分钟

这篇文章的目的是为了给自己更清晰地罗列出优秀的前端学习资源,以备日后需要时随时参考。

规范

最原始、权威的第一手资料,内容非常详细、严谨。

  • ECMAScript specification

  • HTML draft

  • HTML specification

  • DOM specification

  • CSSOM specification

  • W3C standards

  • The WHATWG specification index.

阅读全文 »
1…567…10
Bill Wen

Bill Wen

less is more
57 日志
70 标签
GitHub
Creative Commons
0%
© 2018 – 2020 Bill Wen | 195k | 5:55
由 Hexo 强力驱动
|
主题 – NexT.Pisces