Skip to content

Commit

Permalink
Merge pull request #394 from jprichardson/buffer
Browse files Browse the repository at this point in the history
Use Buffer.allocUnsafe() in enviroments that support it
  • Loading branch information
jprichardson committed Mar 17, 2017
2 parents 84717b8 + a86d8b4 commit ed5dc63
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/copy-sync/copy-file-sync.js
Expand Up @@ -3,7 +3,7 @@
const fs = require('graceful-fs')

const BUF_LENGTH = 64 * 1024
const _buff = new Buffer(BUF_LENGTH)
const _buff = require('../util/buffer')(BUF_LENGTH)

function copyFileSync (srcFile, destFile, options) {
const overwrite = options.overwrite
Expand Down
3 changes: 2 additions & 1 deletion lib/move-sync/index.js
Expand Up @@ -5,6 +5,7 @@ const path = require('path')
const copySync = require('../copy-sync').copySync
const removeSync = require('../remove').removeSync
const mkdirpSync = require('../mkdirs').mkdirsSync
const buffer = require('../util/buffer')

function moveSync (src, dest, options) {
options = options || {}
Expand Down Expand Up @@ -60,7 +61,7 @@ function moveSyncAcrossDevice (src, dest, overwrite) {

function moveFileSyncAcrossDevice (src, dest, overwrite) {
const BUF_LENGTH = 64 * 1024
const _buff = new Buffer(BUF_LENGTH)
const _buff = buffer(BUF_LENGTH)

const flags = overwrite ? 'w' : 'wx'

Expand Down
10 changes: 10 additions & 0 deletions lib/util/buffer.js
@@ -0,0 +1,10 @@
module.exports = function (size) {
if (typeof Buffer.allocUnsafe === 'function') {
try {
return Buffer.allocUnsafe(size)
} catch (e) {
return new Buffer(size)
}
}
return new Buffer(size)
}

0 comments on commit ed5dc63

Please sign in to comment.