Why this JS code writting to a hole file so slow on Windows?












0















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)


Output from a 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


And on Ubuntu 16.04 in a virtual machine running on the Windows above



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


Both using the latest node executable



The results are the same even if I remove the truncate part, which is what I thought the cause of slow



Please help me improve it

Thanks










share|improve this question













migrated from codereview.stackexchange.com 9 hours ago


This question came from our site for peer programmer code reviews.



















  • Maybe you disabled write caching on the disk in Windows 10?

    – ChatterOne
    11 hours ago











  • @ChatterOne 'Enable write caching...' is checked for all disks as default

    – ZED.CWT
    11 hours ago











  • That is interesting, because you see that the second time you write to the file, it's almost as fast as the Linux equivalent (a bit slower, but nothing terrible). So, that would suggest something to do with caching. I don't have a Windows PC ready, maybe I'll try later, but on Mac OS it's not slow.

    – ChatterOne
    10 hours ago











  • It might be possible that Linux (or a part of the stack below your code and the OS) optimizes things by making the file a sparse file, while Windows doesn't?

    – AKX
    8 hours ago











  • You should also try whether different file systems on the Linux box have different performance characteristics here.

    – AKX
    8 hours ago
















0















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)


Output from a 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


And on Ubuntu 16.04 in a virtual machine running on the Windows above



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


Both using the latest node executable



The results are the same even if I remove the truncate part, which is what I thought the cause of slow



Please help me improve it

Thanks










share|improve this question













migrated from codereview.stackexchange.com 9 hours ago


This question came from our site for peer programmer code reviews.



















  • Maybe you disabled write caching on the disk in Windows 10?

    – ChatterOne
    11 hours ago











  • @ChatterOne 'Enable write caching...' is checked for all disks as default

    – ZED.CWT
    11 hours ago











  • That is interesting, because you see that the second time you write to the file, it's almost as fast as the Linux equivalent (a bit slower, but nothing terrible). So, that would suggest something to do with caching. I don't have a Windows PC ready, maybe I'll try later, but on Mac OS it's not slow.

    – ChatterOne
    10 hours ago











  • It might be possible that Linux (or a part of the stack below your code and the OS) optimizes things by making the file a sparse file, while Windows doesn't?

    – AKX
    8 hours ago











  • You should also try whether different file systems on the Linux box have different performance characteristics here.

    – AKX
    8 hours ago














0












0








0








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)


Output from a 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


And on Ubuntu 16.04 in a virtual machine running on the Windows above



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


Both using the latest node executable



The results are the same even if I remove the truncate part, which is what I thought the cause of slow



Please help me improve it

Thanks










share|improve this question














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)


Output from a 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


And on Ubuntu 16.04 in a virtual machine running on the Windows above



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


Both using the latest node executable



The results are the same even if I remove the truncate part, which is what I thought the cause of slow



Please help me improve it

Thanks







javascript performance node.js windows






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 12 hours ago









ZED.CWTZED.CWT

411




411




migrated from codereview.stackexchange.com 9 hours ago


This question came from our site for peer programmer code reviews.









migrated from codereview.stackexchange.com 9 hours ago


This question came from our site for peer programmer code reviews.















  • Maybe you disabled write caching on the disk in Windows 10?

    – ChatterOne
    11 hours ago











  • @ChatterOne 'Enable write caching...' is checked for all disks as default

    – ZED.CWT
    11 hours ago











  • That is interesting, because you see that the second time you write to the file, it's almost as fast as the Linux equivalent (a bit slower, but nothing terrible). So, that would suggest something to do with caching. I don't have a Windows PC ready, maybe I'll try later, but on Mac OS it's not slow.

    – ChatterOne
    10 hours ago











  • It might be possible that Linux (or a part of the stack below your code and the OS) optimizes things by making the file a sparse file, while Windows doesn't?

    – AKX
    8 hours ago











  • You should also try whether different file systems on the Linux box have different performance characteristics here.

    – AKX
    8 hours ago



















  • Maybe you disabled write caching on the disk in Windows 10?

    – ChatterOne
    11 hours ago











  • @ChatterOne 'Enable write caching...' is checked for all disks as default

    – ZED.CWT
    11 hours ago











  • That is interesting, because you see that the second time you write to the file, it's almost as fast as the Linux equivalent (a bit slower, but nothing terrible). So, that would suggest something to do with caching. I don't have a Windows PC ready, maybe I'll try later, but on Mac OS it's not slow.

    – ChatterOne
    10 hours ago











  • It might be possible that Linux (or a part of the stack below your code and the OS) optimizes things by making the file a sparse file, while Windows doesn't?

    – AKX
    8 hours ago











  • You should also try whether different file systems on the Linux box have different performance characteristics here.

    – AKX
    8 hours ago

















Maybe you disabled write caching on the disk in Windows 10?

– ChatterOne
11 hours ago





Maybe you disabled write caching on the disk in Windows 10?

– ChatterOne
11 hours ago













@ChatterOne 'Enable write caching...' is checked for all disks as default

– ZED.CWT
11 hours ago





@ChatterOne 'Enable write caching...' is checked for all disks as default

– ZED.CWT
11 hours ago













That is interesting, because you see that the second time you write to the file, it's almost as fast as the Linux equivalent (a bit slower, but nothing terrible). So, that would suggest something to do with caching. I don't have a Windows PC ready, maybe I'll try later, but on Mac OS it's not slow.

– ChatterOne
10 hours ago





That is interesting, because you see that the second time you write to the file, it's almost as fast as the Linux equivalent (a bit slower, but nothing terrible). So, that would suggest something to do with caching. I don't have a Windows PC ready, maybe I'll try later, but on Mac OS it's not slow.

– ChatterOne
10 hours ago













It might be possible that Linux (or a part of the stack below your code and the OS) optimizes things by making the file a sparse file, while Windows doesn't?

– AKX
8 hours ago





It might be possible that Linux (or a part of the stack below your code and the OS) optimizes things by making the file a sparse file, while Windows doesn't?

– AKX
8 hours ago













You should also try whether different file systems on the Linux box have different performance characteristics here.

– AKX
8 hours ago





You should also try whether different file systems on the Linux box have different performance characteristics here.

– AKX
8 hours ago












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55284532%2fwhy-this-js-code-writting-to-a-hole-file-so-slow-on-windows%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55284532%2fwhy-this-js-code-writting-to-a-hole-file-so-slow-on-windows%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to make a Squid Proxy server?

第一次世界大戦

Touch on Surface Book