- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我从一些非特定来源听说使用 shift()
的组合和 pop()
比使用 splice()
快.我还没有运行基准测试,而且很可能不会因为我出城而有一段时间,但这是真的吗,如果是这样,为什么?
最佳答案
如果您不需要保留数组的顺序,请使用 .pop()
比 .splice()
快得多..splice()
将删除给定索引处的元素。然后发生的情况是,在此之后数组中的每个其他元素都需要将其在数组中的位置减 1。如果您的数组很大并且删除元素的索引很小,这可能会很慢。
使用 .pop()
,您可以完全删除重新索引所有内容的过程。您可以使用 .pop()
,而不是删除给定索引处的元素。从数组中删除最后一项。从这里开始,您需要做的就是将要删除的项目替换为使用 .pop()
获得的项目。 .如前所述,这当然意味着您的元素的顺序没有得到维护。
代码示例:.splice()
:
var list:Array = [0,1,2,3,4,5];
list.splice(2, 1);
trace(list); // 0,1,3,4,5
.pop()
:
var last:* = list.pop();
list[2] = last;
trace(list); // 0,1,5,4 - Notice we move 5 to the index 2 and lose the order.
var list:Array = [];
function fillList():void
{
for(var i = 0; i < 200000; i++) list.push(i);
}
function emptyViaSplice():void
{
fillList();
var t:Number = getTimer();
while(list.length > 0)
{
list.splice(0, 1);
}
trace("emptyViaSplice: " + (getTimer() - t));
}
function emptyViaPop():void
{
fillList();
var t:Number = getTimer();
while(list.length > 0)
{
if(list.length == 1) list.pop();
else
{
var l:* = list.pop();
list[0] = l;
}
}
trace("emptyViaPop: " + (getTimer() - t));
}
emptyViaSplice(); // 12153 ms
emptyViaPop(); // 37 ms
关于actionscript-3 - Splice 比 Shift + Pop 快吗?为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15052782/
我正在尝试实现 Intellij 之类的双类导航。我发现它被称为“快速打开文件”的“序列键绑定(bind)”。 因此,我在 .sublime-keymap --- USER 中添加了这一行 { "
this.x = (Math.random()*canvasWidth); this.y = (Math.random()*canvasHeight); (1) this.shift = {x: th
我目前正在研究编译器,据我了解,在 LR(0) 中,有时会出现“shift/reduce”或“reduce/reduce”冲突,但不可能出现“shift/shift”冲突!为什么我们不能发生“转变/转
我希望使用 AutoHotKey 来修改我的 shift 键的功能。 Steve Losh 的博客条目 here 中描述了该功能。 .具体来说,我希望我的 shift 键执行以下操作: 如果 LShi
在我的Redis数据库中,我有一个以prefix:格式保存的键列表,所以示例数据库看起来像这样:。问题是,当我尝试删除前缀:1时,因为无论出于什么原因,我都需要删除IPSUM,在索引之间留下了一个间隙
在我的Redis DB中,我有一个以prefix:格式保存的键列表,因此示例数据库如下所示:。问题是,当我尝试删除前缀:1时,因为无论出于什么原因,我都需要删除IPSUM,在索引之间留下了一个间隙--
Listview 控件中是否有可以上下移动项目的功能? 最佳答案 我没有太多使用 TListView(我主要使用数据库网格),我把你的问题作为学习一些东西的机会。下面的代码是结果,它比大卫的答案更直观
我有点难以发现这两种操作的不同之处。所以我读到算术移位与逻辑移位基本相同,唯一的区别是它以某种方式保持最高位或有符号位。 所以当我在 101110 上做 LSL #2 时假设这个二进制是一个无符号的,
我正在尝试使用 shift+shift 配置键映射,这可能吗? 我试过: But this maps shift + S and this that maps to unknown 最佳答案 您不
我正在努力在 vim 中绑定(bind) Shift+- 组合。我已经尝试了以下两种选项,但都没有可用的选项,并且谷歌搜索没有带来任何相关信息。 nnoremap - nnoremap - 我正在
我正在使用基于Gallery 的Cover-flow,正如建议的那样here . 是否有可以捕获的事件表明在我的画廊/封面流中进行了一次移位(向左或向右)? 需要明确的是,如果我给画廊一个像样的滑动并
这个问题在这里已经有了答案: Cocoa - NSEvent Respond to the SHIFT key? (5 个答案) 关闭 3 年前。 我正在制作这个应用程序,我想检测是否检测到 shi
我不允许在我的 C 代码中使用浮点变量(出于性能原因)。我想进行整数除法运算,同时我想尽可能地防止除法损失。 我的理解是,如果分子很大,除法运算会产生很好的结果。目前我正在对分子进行左移操作以使其成为
我有一个文本输入,当用户按下 shift(keydown)并绑定(bind)一个监听器以监听 shift 键时,它现在变得透明 即。 $('#foo').keydown(function(){
我有一个包含 shift 命令的 bash 脚本。 它在 bash 中运行正常,但在 zsh 环境中,shift 命令似乎不同。 zsh $ shift shift: shift count must
我正在使用 Fedora 29 中包含的 vim: VIM - Vi IMproved 8.1(2018 年 5 月 18 日,2019 年 2 月 4 日 14:19:12 编译) 我刚刚注意到按
我有一个包含 shift 命令的 bash 脚本。 它在 bash 中运行正常,但在 zsh 环境中,shift 命令似乎不同。 zsh $ shift shift: shift count must
我正在使用 Fedora 29 中包含的 vim: VIM - Vi IMproved 8.1(2018 年 5 月 18 日,2019 年 2 月 4 日 14:19:12 编译) 我刚刚注意到按
我可以使用 UITextViewDelegate 或 NSNotificationCenter 来检测用户何时点击 Keyboard 上的字符键将文本输入到 UITextView. 但我的应用程序需要
当我尝试单击 shift + tab 组合时, Shiftkey 被触发 $("#buttonZZ").on("keydown",function (eve) { if (eve.keyCod
我是一名优秀的程序员,十分优秀!