Skip to content

Commit 938d165

Browse files
committedJun 25, 2017
Implemented by using N-API, update to v2.0.0
1 parent 5990620 commit 938d165

13 files changed

+3118
-1448
lines changed
 

‎README.md

+81-68
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ StringBuilder for Node.js
33

44
An easy and fast in-memory string builder for Node.js.
55

6+
NOTICE: N-API is a new experimental feature in Node.js 8. Currently, it can be used by adding `--napi-modules` option when executing Node.js 8.
7+
68
## Code Example
79

810
```javascript
911
const StringBuilder = require('node-stringbuilder');
1012
var sb = new StringBuilder('Hi');
1113
sb.appendLine(',').append('This is a simple example demonstrating how to use this module.');
1214
console.log(sb.toString()); // Hi,
13-
// This is a simple example demonstrating how to use this module.
15+
// This is a simple example demonstrating how to use this module.
1416
sb.insert('Text can be added into any position of this builder.');
1517
sb.replace(53, 118, 'Or replace the existing text.');
1618
console.log(sb.toString()); // Text can be added into any position of this builder.HOr replace the existing text.
@@ -22,8 +24,11 @@ console.log(sb.clone().reverse().toString()); // .gnirts gnitsixe eht ecalper ro
2224
console.log(sb.toString(0, 19)); // string can be added
2325
console.log(sb.length()); // 86
2426
console.log(sb.count()); // 15
25-
console.log(sb.indexOf('is')); // [ 43, 72 ]
26-
console.log(sb.lastIndexOf('is')); // [ 72, 43 ]
27+
console.log(sb.indexOf('is')); // Uint32Array [ 43, 72 ]
28+
console.log(sb.indexOfSkip('is')); // Uint32Array [ 43, 72 ]
29+
console.log(sb.lastIndexOf('is')); // Uint32Array [ 72, 43 ]
30+
console.log(sb.indexOfRegExp(/is/g)); // { index: [ 43, 72 ], lastIndex: [ 45, 74 ] }
31+
console.log(sb.repeat().indexOf('is')); // Uint32Array [ 43, 72, 129, 158 ]
2732
sb.substring(11, 37);
2833
console.log(sb.toString()); // be added into any position
2934
console.log(sb.equalsIgnoreCase('be Added into Any position')); // true
@@ -47,6 +52,7 @@ npm install node-stringbuilder --save
4752

4853
## Features
4954

55+
* Implemented with N-API.
5056
* Operating strings in a scalable buffer.
5157
* Multiple types of data are allowed to input.
5258
* Strings
@@ -299,7 +305,13 @@ Search substrings from the head,
299305

300306
```javascript
301307
var indexArray = sb.indexOf('string');
302-
var indexArray2 = sb.indexOf(/string/g, offset, limit);
308+
var indexArray2 = sb.indexOf('string', offset, limit);
309+
```
310+
311+
Search substrings from the head by using RegExp,
312+
313+
```javascript
314+
var indexArray = sb.indexOf(/string/g);
303315
```
304316

305317
Search substrings from the end,
@@ -308,8 +320,6 @@ Search substrings from the end,
308320
var indexArray = sb.lastIndexOf('string');
309321
```
310322

311-
RegExp is not supported in `lastIndexOf` method.
312-
313323
### Equals
314324

315325
Determine whether the two strings are the same.
@@ -359,69 +369,73 @@ npm install
359369
npm run benchmark
360370
```
361371

362-
Here is my report,
372+
Here is my result,
363373

364374
```bash
365-
Append
366-
- 44 milliseconds
367-
✓ natively append text 1000000 times (44ms)
368-
- 259 milliseconds
369-
✓ Using StringBuilder to append text 1000000 times (262ms)
370-
- 43 milliseconds
371-
✓ Using StringBuilder to append text 1000000 times repeatly (44ms)
372-
373-
Insert
374-
- 53 milliseconds
375-
✓ natively insert text 10000 times (53ms)
376-
- 10 milliseconds
377-
✓ Using StringBuilder to insert text 10000 times
378-
379-
Delete
380-
- 1362 milliseconds
381-
✓ natively delete text 5000 times (1364ms)
382-
- 90 milliseconds
383-
✓ Using StringBuilder to delete text 5000 times (91ms)
384-
385-
Replace
386-
- 1485 milliseconds
387-
✓ natively replace text 5000 times (1486ms)
388-
- 90 milliseconds
389-
✓ Using StringBuilder to replace text 5000 times (91ms)
390-
391-
Replace Pattern
392-
- 39 milliseconds
393-
✓ natively replace text by using a RegExp pattern (39ms)
394-
- 666 milliseconds
395-
✓ Using StringBuilder to replace text by using a pattern (673ms)
396-
397-
Equals
398-
- 1 milliseconds
399-
✓ natively check the equal 50000 times
400-
- 14 milliseconds
401-
✓ Using StringBuilder to check the equal 50000 times
402-
403-
EqualsIgnoreCase
404-
- 6 milliseconds
405-
✓ natively check the equal 50000 times
406-
- 53 milliseconds
407-
✓ Using StringBuilder to check the equal 50000 times (54ms)
408-
409-
IndexOf
410-
- 36 milliseconds
411-
✓ natively search text
412-
- 212 milliseconds
413-
✓ Using StringBuilder to search text (218ms)
414-
415-
Reverse
416-
- 10 milliseconds
417-
✓ natively reverse text
418-
- 9 milliseconds
419-
✓ Using StringBuilder to reverse text
420-
```
421-
422-
According to the result of benchmark, if you just want to append strings, please append them by using native operator `+` instead of this module.
423-
424-
And although this module uses Boyer-Moore-MagicLen for searching strings, it still slower than the native implement because the Javascript code is not efficiency enough. It needs to be moved to the native layer(C/C++ code) in the future.
375+
Append
376+
- 43 milliseconds
377+
✓ Natively append text 1000000 times (43ms)
378+
- 567 milliseconds
379+
✓ Use StringBuilder to append text 1000000 times (567ms)
380+
- 1278 milliseconds
381+
✓ Use StringBuilder to insert text 1000000 times at the end (1287ms)
382+
- 17 milliseconds
383+
✓ Use StringBuilder to append text 1000000 times repeatly
384+
385+
Insert
386+
- 92 milliseconds
387+
✓ Natively insert text 10000 times (92ms)
388+
- 10 milliseconds
389+
✓ Use StringBuilder to insert text 10000 times
390+
391+
Delete
392+
- 1427 milliseconds
393+
✓ Natively delete text 5000 times (1429ms)
394+
- 87 milliseconds
395+
✓ Use StringBuilder to delete text 5000 times (88ms)
396+
397+
Replace
398+
- 1511 milliseconds
399+
✓ Natively replace text 5000 times (1513ms)
400+
- 85 milliseconds
401+
✓ Use StringBuilder to replace text 5000 times (86ms)
402+
403+
Replace Pattern
404+
- 37 milliseconds
405+
✓ Natively replace text with the same length by using a RegExp pattern
406+
- 20 milliseconds
407+
✓ Use StringBuilder to replace text with the same length by using a pattern
408+
- 35 milliseconds
409+
✓ Natively replace text by using a RegExp pattern
410+
- 29 milliseconds
411+
✓ Use StringBuilder to replace text by using a pattern
412+
413+
Equals
414+
- 2 milliseconds
415+
✓ Natively check the equal 50000 times
416+
- 13 milliseconds
417+
✓ Use StringBuilder to check the equal 50000 times
418+
419+
EqualsIgnoreCase
420+
- 21 milliseconds
421+
✓ Natively check the equal 50000 times
422+
- 19 milliseconds
423+
✓ Use StringBuilder to check the equal 50000 times
424+
425+
IndexOf
426+
- 65 milliseconds
427+
✓ Natively search text (65ms)
428+
- 2 milliseconds
429+
✓ Use StringBuilder to search text
430+
431+
Reverse
432+
- 516 milliseconds
433+
✓ Natively reverse text (516ms)
434+
- 14 milliseconds
435+
✓ Use StringBuilder to reverse text
436+
```
437+
438+
According to the result of benchmark, if you just want to append a few different strings, please append them by using native operator `+` instead of this module.
425439

426440
## License
427441

@@ -430,6 +444,5 @@ And although this module uses Boyer-Moore-MagicLen for searching strings, it sti
430444
## To Do
431445

432446
* More test cases
433-
* Use C/C++ lib to improve the performance
434447

435448
If you can help me do this as collaborators, I will be grateful.

0 commit comments

Comments
 (0)