Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion benchmarks/helpers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const WORDS = require('../spec/helpers/words')
const Random = require('random-seed')
const random = new Random(Date.now())
const {Point, Range} = require('..')

exports.getRandomText = function (sizeInKB) {
const goalLength = sizeInKB * 1024
const goalLength = Math.round(sizeInKB * 1024)

let length = 0
let lines = []
Expand Down Expand Up @@ -41,3 +42,19 @@ exports.getRandomText = function (sizeInKB) {

return lines.join('\n') + '\n'
}

exports.getRandomRange = function (buffer) {
const start = getRandomPoint(buffer)
const end = getRandomPoint(buffer)
if (end.isLessThan(start)) {
return new Range(end, start)
} else {
return new Range(start, end)
}
}

function getRandomPoint (buffer) {
const row = random(buffer.getLineCount())
const column = random(buffer.lineLengthForRow(row))
return new Point(row, column)
}
1 change: 1 addition & 0 deletions benchmarks/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env node

require('./construction')
require('./mutation')
21 changes: 21 additions & 0 deletions benchmarks/mutation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const helpers = require('./helpers')
const TextBuffer = require('..')

let text = helpers.getRandomText(100)
let buffer = new TextBuffer({text})
let displayLayer = buffer.addDisplayLayer({})

let t0 = Date.now()

for (let i = 0; i < 1000; i++) {
buffer.setTextInRange(
helpers.getRandomRange(buffer),
helpers.getRandomText(0.5)
)
}

let t1 = Date.now()

console.log('Mutation')
console.log('------------')
console.log('TextBuffer: %s ms', t1 - t0)
29 changes: 18 additions & 11 deletions src/helpers.coffee
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
Point = require './point'

SpliceArrayChunkSize = 100000

MULTI_LINE_REGEX_REGEX = /\\r|\\n|\r|\n|^\[\^|[^\\]\[\^/

module.exports =
spliceArray: (originalArray, start, length, insertedArray=[]) ->
if insertedArray.length < SpliceArrayChunkSize
originalArray.splice(start, length, insertedArray...)
spliceArray: (array, start, removedCount, insertedItems=[]) ->
oldLength = array.length
insertedCount = insertedItems.length
removedCount = Math.min(removedCount, oldLength - start)
lengthDelta = insertedCount - removedCount
newLength = oldLength + lengthDelta

if lengthDelta > 0
array.length = newLength
for i in [(newLength - 1)..(start + insertedCount)] by -1
array[i] = array[i - lengthDelta]
else
removedValues = originalArray.splice(start, length)
for chunkStart in [0..insertedArray.length] by SpliceArrayChunkSize
chunkEnd = chunkStart + SpliceArrayChunkSize
chunk = insertedArray.slice(chunkStart, chunkEnd)
originalArray.splice(start + chunkStart, 0, chunk...)
removedValues
for i in [(start + insertedCount)...newLength] by 1
array[i] = array[i - lengthDelta]
array.length = newLength

for value, i in insertedItems by 1
array[start + i] = insertedItems[i]
return

newlineRegex: /\r\n|\n|\r/g

Expand Down