-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer性能.js
60 lines (54 loc) · 1.52 KB
/
buffer性能.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// const start = new Date().getTime();
// let num = 100;
// for(let i = 0; i < 100000000; i++){
// num ** 2
// }
// console.log('耗时', new Date().getTime() - start);
// const start = new Date().getTime();
// let num = Buffer.alloc(1);
// num.writeInt8(100, 0);
// for(let i = 0; i < 100000000; i++){
// num[0] ** 2
// }
// console.log('buffer耗时', new Date().getTime() - start);
// http body为字符串(socket io 会把数据转换为Buffer,以进行二进制数据传输)
// var http = require('http');
// var helloworld = "";
// for (var i = 0; i < 1024 * 10; i++) {
// helloworld += "a";
// }
// helloworld = new Buffer.from(helloworld);
// http.createServer(function (req, res) {
// res.writeHead(200);
// res.end(helloworld);
// }).listen(8001);
const http = require('http');
http.createServer(function (req, res) {
if(req.url === '/ajax') {
res.writeHead(200);
const reqChunks = [];
req.on('data', buf => {
reqChunks.push(buf);
})
req.on('end', () => {
const buffer = Buffer.concat(reqChunks);
console.log(buffer, buffer.toString());
})
const resBody = JSON.stringify({
code: 0,
msg: '成功',
data: {
url: req.url
}
})
res.end(resBody);
}
else {
res.writeHead(200);
const resBody = JSON.stringify({
code: 404,
msg: '无效路由'
})
res.end(resBody);
}
}).listen(8001);