Appearance
Interview Questions
1. Is JavaScript single-threaded?
Yes, JavaScript is a single-threaded language. This means that it has only one call stack and one memory heap. Only one set of instructions is executed at a time.
Also, JavaScript is synchronous and blocking in nature, meaning that code is executed line by line and one task must be completed before the next one begins.
However, JavaScript also has asynchronous capabilities, which allow certain operations to be executed independently of the main execution thread. This is commonly achieved through mechanisms like callbacks, promises, async/await, and event listeners. These asynchronous features enable JavaScript to handle tasks such as fetching data, handling user input, and performing I/O operations without blocking the main thread, making it suitable for building responsive and interactive web applications.
JavaScript是单线程的吗?
是的, JavaScript是单线程语言。 这意味着它只有一个调用栈和内存堆。一次只执行一组指令。 此外,本质上 JavaScript是同步和阻塞的,这意味着代码是逐行执行的,并且必须完成一个任务才能开始下一个任务。 然而,JavaScript也具有异步能力,这允许有些操作独立于主执行线程执行。这通常是通过回调、promise、async/await和事件侦听器等机制来实现的。这些异步特性让JavaScript可以处理诸如获取数据,处理用户输入和执行I/O操作等任务,而不会阻塞主线程,使其适合于构建响应式和交互式的Web应用程序。
2. Explain the main component of the JavaScript Engine and how it works.

Every browser has a JavaScript engine that executes the JavaScript code and converts it into machine code.
When JavaScript code is executed, the parser first reads the code and produces an AST, and stores it in memory. The interpreter then processes this AST and generates bytecode or machine code, which is executed by the computer.
The profiler is a component of a JavaScript engine that monitors the execution of the code.
Bytecode is used by optimizing the compiler along with profiling data. "Optimizing compiler" or Just-in-time(JIT) compiler makes certain assumptions based on profiling data and produces highly optimized machine code.
Sometimes there is a case where the 'optimization' assumption is incorrect and then it goes back to the previous version via the "Deoptimize" phase (where it actually becomes the overhead to us)
JavaScript Engine usually optimizes "hot functions" and uses inline caching techniques to optimize the code.
During this process, the call stack keeps track of the currently executing functions, and the memory heap is used for memory allocation.
Finally, the garbage collector comes into play to manage memory by reclaiming memory from unused objects.
解释一下JavaScript引擎的主要组件及其工作原理。
每个浏览器都有一个JavaScript引擎,用于执行JavaScript代码,并将其转换为机器码。 当JavaScript代码被执行时,解析器(parser)首先读取代码并生成一个抽象语法树(AST),并将其存储到内存中。然后解释器(interpreter)处理这个AST,并生成由计算机执行的字节码或机器码。 分析器(profiler)是JavaScript引擎的一个组件,它负责监控代码的执行。 字节码被用来优化编译器(compiler)和分析数据。“优化编译器”或即时(just-in-time)编译器基于分析数据做出某些假设,并生成高度优化的机器码。 有时会出现“优化”假设不正确的情况,然后通过“反优化”阶段回到前一个版本(这实际上对我们来说是开销) JS引擎通常会优化“热函数”,并使用内联缓存技术来优化代码。 在此过程中,调用栈将跟踪当前正在执行的函数,内存堆用于内存分配。 最后,垃圾回收器发挥作用,通过从未使用的对象回收内存来管理内存。
3. Explain the Event loop in JavaScript.

The Event loop is a core component of the JavaScript runtime environment. It is responsible for scheduling and executing asynchronous tasks. The event loop works by continuously monitoring two queues: the call stack and the event queue.
The call stack is stack(LIFO, Last In, First Out) data structure that stores the functions that are currently being executed (stores the execution context created during the code execution).
Web APIs is the place where the async operations (setTimeout, fetch requests, promises) with their callbacks are waiting to complete. It borrows the thread from the thread pool to complete the task in the background without blocking the main thread.
The job queue(or microtasks) is a FIFO(First In, First Out) structure that holds the callbacks of async/await, promises, process.nextTick() that are ready to be executed. For example, the resolve or reject callbacks of a fulfilled promise are enqueued in the job queue.
The task queue (or macrotasks) is a FIFO(First In, First Out) structure that holds the callbacks of async operations (timer like setInterval, setTimeout) that are ready to be executed. For example, the callback of a timed-out setTimeout() -ready to be executed - is enqueued in the task queue.
The Event loop permanently monitors whether the call stack is empty. If the call stack is empty, the event loop looks into the job queue or task queue and dequeues any callback ready to be executed into the call stack.
解释一下JavaScript中的事件循环。
事件循环式是JavaScript运行时环境中的一个核心组成部分。它的主要责任是负责异步任务的调度和执行。事件循环工作中持续监控两个队列,调用栈和事件队列。 调用栈是一个栈(LIFO)的数据结构,它储存当前正在执行的函数(储存代码执行期间创建的执行上下文)。 Web API的异步操作(setTimeout,fetch请求,premises)以及回调,等在(调用栈)完成的地方。它从线程池借用线程在后台完成任务,而不会阻塞主线程。 工作队列(或微任务)是一个先进先出(FIFO)的结构,它保存着准备好执行的async、await、promises、process。nextTick()的回调。例如,一个已完成的promise的resolve或reject回调被放入任务队列。 任务队列(或宏任务)是一个先进先出(FIFO)的结构,它保存了准备好执行的异步操作(setInterval,setTimeout)的回调。例如,一个超时的setTimeout的回调 -- 准备执行 -- 被放入任务队列。 事件循环永久性的监控调用栈是否为空,如果调用栈是空的,事件循环机制就会查看工作队列或任务队列,并将准备好执行的回调调出队到调用栈。
