The answers below are for your reference only. Most of the questions are about applying certain concepts flexibly, not memorizing them by heart
1, nodejs
1.1. Nodejs threads
QA: We all know that NodeJS is single-threaded, but if you look at the processes nodeJS starts, you’ll see that there are actually seven threads. What are these 7 threads responsible for?
Single-threaded simply because there is only one js code executing, which is also called the main thread. But things like GC are not executed on the main thread, so the 7 threads include:
- Main thread: compiles and executes code.
- Compile/optimize threads: Code can be optimized while the main thread is executing.
- Analyzer thread: Logs profiling code runtime to provide a basis for optimizing code execution by the Crankshaft.
- Several threads of garbage collection.
- Libuv event-loop thread
- There is also an alternate thread pool, provided by Libuv, which will not count to seven threads if it is not used
1.2, v8
QA: Know v8? V8 also has a thing called context that we switch whenever we need to switch contexts, such as one worker to another. But context switching involves the stack and other operations that can be time consuming. So what techniques are used in V8 to optimize this context switch?
If you have read v8’s basic official documentation, you know that V8 uses a lot of caching to increase the cost of context switching.
[c]V8 advanced learning
1.3 nodeJS monitoring
QA: Have you done Nodejs process status monitoring? In addition to the CPU and memory that are generally monitored, the average scheduling time of event loop may also need to be monitored. Then, is there any way to count the delay time of event loop?
This topic examines the understanding of Event loop. If we know the principle of Event loop, we can actively add a timer, and then calculate the time of adding the timer and the time difference between the callback function being called and the timeout time set by the timer, which are the delay time of event loop.
Pseudo-code implementation:
Const now = date.now () // If you need more precision, use process.hrtime.bigint()setTimeout() => {const cost = date.now () -now const eventloopDelay = cost - 100}, 100)Copy the code
1.4. Task Queue
QA: Tell me about the print result of the following code:
const EventEmitter = require('events')
class EE extends EventEmitter {}
const yy = new EE()
console.log('Test started')
yy.on('event', () => console.log('I'm an event callback emitted by EventEmitter'))
setTimeout(() => {
console.log('Timer callback 1 to expire after 0 ms')
process.nextTick(() => console.log('I am a microtask with 0 ms timer 1 plug'))}, 0)setTimeout(() => {
console.log('Timer callback 2 to expire after 0 ms')
process.nextTick(() => console.log('I am a microtask with 0 ms timer 2 plug'))}, 0)setImmediate(() => console.log('Immediate immediate callback'))
process.nextTick(() => console.log('Process. nextTick's first callback'))
new Promise((resolve) => {
console.log('I am a promise')
}).then(() => {
yy.emit('event')
process.nextTick(() => console.log('Process. nextTick second callback'))
console.log('Promise first callback')
})
.then(() => console.log('Promise second callback'))
console.log('End of test? ')
Copy the code
Some of the most confusing and frustrating concepts in THE JS language are:
1.5. Middleware of Express
QA: Curl 127.0.0.1:3000/ API /test1
const express = require('express')
const app = express()
const sleep = (mseconds) => new Promise((resolve) => setTimeout(() => {
console.log('sleep timeout... ')
resolve()
}, mseconds))
app.use(async (req, res, next) => {
console.log('I am the first middleware')
const startTime = Date.now()
console.log(`================ start ${req.method} ${req.url}`, { query: req.query, body: req.body });
next()
const cost = Date.now() - startTime
console.log(`================ end ${req.method} ${req.url} ${res.statusCode} - ${cost} ms`)
})
app.use((req, res, next) => {
console.log('I am the second middleware')
next()
console.log('second middleware end calling')
})
app.get('/api/test1', async(req, res, next) => {
console.log('I am the router middleware => /api/test1')
await sleep(2000)
res.status(200).send('hello')
})
app.listen(3000)
console.log('server listening at port 3000')
Copy the code
Some of the most confusing and frustrating concepts in THE JS language are:
2, javascript,
2.1. Usage of modifiers
QA: Use the following code to use the functions of the decorator to abstract all the common parts into a single decorator that can be easily used in individual requests
class allApisClass {
async fetchGoods(params) {
Toast.loading('Just a moment... ')
try {
const info = await fetchHandle(fetchGoods, params)
Toast.close()
return info
} catch (err) {
Toast.close()
Toast.error(err.msg)
}
}
}
Copy the code
For example, if the abstract decorator is: decorator, I can simply use:
class allApisClass {
@decortor()
async fetchGoods(params){
const info = await fetchHandle(fetchGoods, params)
return info
}
}
Copy the code
Implement the corresponding decorator function:
function decorator(target, propertyKey, descriptor) {
}
Copy the code
Do you think decorators are easy to learn?
2.2. Questions about page access time
QA: Do you think componentWillUnMount makes sense when using the React framework? Why is that? If you were doing it, how would you do it?
Statistics page access time is from page rendering to page closing time, if the use of componentWillUnMount to statistics page closing time is not statistical, because when the page is closed will not trigger the event, the event can be triggered exist premise is the page is still displayed in the browser, so it is not reliable. The listener page can only be closed using the native Unload event.
2.3. Fantastic ideas
QA: How to quickly count the number of occurrences of a certain character in a string? How do I use regular expressions to quickly calculate the most frequently occurring characters?
The answer to the first question is to use split:
str.split('a').length - 1
Copy the code
The answer to the second question is to use the capture group concept of regular expressions: /(\w)\1+/g
const str = 'fhsdfhdsofsifjisffs'
const arr = str.split(' '); Const STR = arr.sort().join() const STR = arr.sort().join()' ')
const value = ' 'Const re = /(\w)\1+/g str.replace(re, const index = 0 // matching character, repeated at least oncefunction ($0.The $1) {
console.log($0.The $1) // If index holds a value less than$0The following operation is performed on the length ofif (index < $0.length) {
index = $0.length;
value = The $1; }})Copy the code