0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-29 08:58:01 -04:00
denoland-deno/runtime/js/40_read_file.js
Aaron O'Mullan 00948a6d68
perf(runtime/fs): optimize readFile by using a single large buffer (#12057)
* perf(runtime/fs): optimize readFile by using a single large buffer
* handle extended/truncated files during read

Allocate an extra byte in our read buffer to detect "overflow" then fallback to unsized readAll for remainder of extended file, this is a slowpath that should rarely happen in practice
2021-09-16 20:28:15 +02:00

43 lines
1,021 B
JavaScript

// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
"use strict";
((window) => {
const core = window.Deno.core;
const { open, openSync } = window.__bootstrap.files;
const { readAllSyncSized, readAllInnerSized } = window.__bootstrap.io;
function readFileSync(path) {
const file = openSync(path);
try {
const { size } = file.statSync();
return readAllSyncSized(file, size);
} finally {
file.close();
}
}
async function readFile(path, options) {
const file = await open(path);
try {
const { size } = await file.stat();
return await readAllInnerSized(file, size, options);
} finally {
file.close();
}
}
function readTextFileSync(path) {
return core.decode(readFileSync(path));
}
async function readTextFile(path, options) {
return core.decode(await readFile(path, options));
}
window.__bootstrap.readFile = {
readFile,
readFileSync,
readTextFileSync,
readTextFile,
};
})(this);