Почему запись этого кода JS в файл дырки происходит так медленно в Windows? - PullRequest
0 голосов
/ 21 марта 2019
var
fs = require('fs'),
target = 'test',
size = 2 * 1024 ** 3,
content = Buffer.from('TEST'),

fd = fs.openSync(target,'w');

console.time('Truncate')
fs.ftruncateSync(fd,size)
console.timeEnd('Truncate')
fs.closeSync(fd)

console.log(fs.statSync(target).size)
fd = fs.openSync(target,'r+')

console.time('Write to 1GB position')
fs.writeSync(fd,content,0,content.length,size / 2)
console.timeEnd('Write to 1GB position')

console.time('Write to 0.5GB position')
fs.writeSync(fd,content,0,content.length,size / 4)
console.timeEnd('Write to 0.5GB position')

console.time('Write to 2GB position')
fs.writeSync(fd,content,0,content.length,size - 100)
console.timeEnd('Write to 2GB position')

console.time('Write to 1GB position again')
fs.writeSync(fd,content,0,content.length,100 + size / 2)
console.timeEnd('Write to 1GB position again')

console.time('Write to 2GB position again')
fs.writeSync(fd,content,0,content.length,size - content.length)
console.timeEnd('Write to 2GB position again')

fs.closeSync(fd)

Вывод из x86_64 Windows 10

Truncate: 4.559ms
2147483648
Write to 1GB position: 5195.480ms
Write to 0.5GB position: 0.096ms
Write to 2GB position: 6506.196ms
Write to 1GB position again: 0.032ms
Write to 2GB position again: 0.023ms

И на Ubuntu 16.04 в виртуальной машине, работающей на Windows выше

Truncate: 0.380ms
2147483648
Write to 1GB position: 0.311ms
Write to 0.5GB position: 0.021ms
Write to 2GB position: 0.330ms
Write to 1GB position again: 0.010ms
Write to 2GB position again: 0.011ms

Оба используют последний исполняемый файл узла

Результаты одинаковы, даже если я удаляю часть truncate, что, как я думал, является причиной медленной

Пожалуйста, помогите мне улучшить его
Спасибо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...