最近,我写了一些代码来过滤字符串中的坏词.而且会有大量的词语被过滤掉.我的代码有效,但性能并不如预期的那么好.
下面的代码只是演示:
方法1:
let list = [ "omg", "bbq", "wth", "hello", "world" ];
let smallString = "wthhello123456worldomg789bbqomgomg";
for (let i = 0; i < list.length; i++) {
smallString = smallString.replace(new RegExp(list[i], "g"), "***");
}
Run Code Online (Sandbox Code Playgroud)
方法2:
let list = [ "omg", "bbq", "wth", "hello", "world" ];
let smallString = "wthhello123456worldomg789bbqomgomg";
for (let i = 0; i < list.length; i++) {
smallString = smallString.split(list[i]).join("***");
}
Run Code Online (Sandbox Code Playgroud)
我还使用jsperf进行性能测试,与split和join或replace进行比较:https://jsperf.com/split-join-vs-replace-dicky
测试结果表明,在更换小字符串时,拆分和连接比替换更快.但是当替换大字符串时它会很慢.(我注意到结果有时会改变)
我真正想要的是什么
我实际上需要一个稳定的功能来更换性能更好的单词.有什么建议吗?它可以是不使用替换或拆分和连接的另一种方法.非常感谢
当用户开始聚焦文本输入时.键盘将从底部移入.开始对焦时如何避免此动画?
我刚刚阅读了TextInput,键盘的文档.但是doc中没有提到相关参数:
就我的情况而言,我有两个具有一对多关系的表(集合、产品)。
通过代码示例,它是:
$collectionProducts = collections::join('products', 'products.collection_id', '=', 'collections.collection_id')->get();
Run Code Online (Sandbox Code Playgroud)
我目前的结果是:
[
Collection 1 Product 1 => [
...Product Info
],
Collection 1 Product 2 => [
...Product Info
],
Collection 2 Product 1 => [
...Product Info
],
Collection 2 Product 3 => [
...Product Info
],
]
Run Code Online (Sandbox Code Playgroud)
我想让结果像:
[
Collection 1 => [
Product 1 => [
...Product Info
],
Product 2 => [
...Product Info
],
],
Collection 2 => [
Product 1 => [
...Product Info
],
Product …Run Code Online (Sandbox Code Playgroud) 我刚刚更改了代码以使代码看起来更好.使用Promise时这两种方法有什么区别吗?我只是害怕它是否会影响程序逻辑.非常感谢.
代码更改前:
function clearTableDemo(tableName) {
return new Promise((resolve, reject) => {
if (db) {
db.executeSql('DELETE FROM '+ tableName, [],
() => { resolve () },
err => { reject() }
);
} else {
reject('db no open');
}
});
}
Run Code Online (Sandbox Code Playgroud)
更改后的代码:( 更新)
function clearTableDemo(tableName) {
if (!db) return Promise.reject('db no open');
db.executeSql('DELETE FROM '+ tableName, [],
() => { return Promise.resolve() },
err => { return Promise.reject(err) }
);
}
Run Code Online (Sandbox Code Playgroud) javascript ×2
eloquent ×1
keyboard ×1
laravel ×1
laravel-5 ×1
performance ×1
php ×1
promise ×1
react-native ×1