added Array.prototype.index(..) a faster version of .indexOf(..)...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-11-27 18:18:07 +03:00
parent 0f7640e11f
commit 7c31ae0765
3 changed files with 79 additions and 24 deletions

View File

@ -222,6 +222,22 @@ object.Mixin('ArrayMixin', 'soft', {
var ArrayProtoMixin =
module.ArrayProtoMixin =
object.Mixin('ArrayProtoMixin', 'soft', {
// A faster version of .indexOf(..)
//
// NOTE: this is not faster when looking for an item not in this,
// for some reason the native .includes(..) and .indexOf(..)
// search for non existant elements about an order of magnirude
// faster than if it existed...
// ...the funny thing is that at least on Crome .lastIndexOf(..)
// is about as fast as this for an item in about the same relative
// location...
// NOTE: this will get depricated as soon as JS redoes its .indexOf(..)
index: function(value){
for(var i = 0; i < this.length && this[i] !== elem; i++){}
return i == this.length ? -1 : i },
// first/last element access short-hands...
//
// .first()
@ -277,8 +293,9 @@ object.Mixin('ArrayProtoMixin', 'soft', {
get len(){
// NOTE: if we don't do .slice() here this can count array
// instance attributes...
// NOTE: .slice() has an added menifit here of removing any
// attributes from the count...
return Object.keys(this.slice()).length },
set len(val){},
// Return a new array with duplicate elements removed...

View File

@ -1,6 +1,6 @@
{
"name": "ig-types",
"version": "5.0.18",
"version": "5.0.19",
"description": "Generic JavaScript types and type extensions...",
"main": "main.js",
"scripts": {

View File

@ -309,31 +309,22 @@ object.Constructor('Queue', Array, {
}))
//---------------------------------------------------------------------
// Task manager...
//
// Externally manage/influence long running tasks...
//
// A task can be:
// - Promise.interactive(..)
// - function(onmsg, ..)
// - object supporting task protocol
//
// The task is controlled by passing messages, default messages include:
// - .stop(..)
//
//
// Task protocol:
// .then(..) - registers a completion handler (a-la Promise)
// .stop(..) - triggers a task to stop
//
//
// Make the ticket more usable...
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Helpres...
// Task ticket...
//
// This lets the client control the task object and receive messages
// from it.
//
// NOTE: this is not intended for direct use...
var TaskTicket =
//module.TaskTicket =
// XXX do we let the user see this???
module.TaskTicket =
object.Constructor('TaskTicket', Promise, {
__data: null,
@ -376,6 +367,11 @@ object.Constructor('TaskTicket', Promise, {
typeof(resolver) == 'function'
&& resolver(resolve, reject) }],
TaskTicket)
// if we got a resolver then it's an internal constructor we are
// not using (likely in base .then(..)) so there is no point in
// moving on...
// NOTE: this may be a potential source of bugs so we need to
// keep tracking this (XXX)
if(typeof(resolver) == 'function'){
return obj }
@ -385,7 +381,6 @@ object.Constructor('TaskTicket', Promise, {
resolve(...arguments) },
function(){
reject(...arguments) })
// setup state...
obj.title = title
obj.task = task
@ -412,8 +407,30 @@ object.Mixin('TaskMixin', 'soft', {
})
// XXX we should keep the API here similar to Queue...
// ...but this is no a queue in principle (internal vs. external
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Task manager...
//
// Externally manage/influence long running tasks...
//
// A task can be:
// - Promise.interactive(..)
// - Queue(..)
// - function(ticket, ..)
// - object supporting task protocol
//
//
// The task is controlled by passing messages, default messages include:
// - .stop(..)
//
//
// Task protocol:
// .then(..) - registers a completion handler (a-la Promise)
// .stop(..) - triggers a task to stop
//
//
// NOTE: we should keep the API here similar to Queue...
// ...but this is not a queue in principle (internal vs. external
// management) so we'll also need to keep them different enough to
// avoid confusion...
var TaskManager =
@ -466,6 +483,7 @@ object.Constructor('TaskManager', Array, events.EventMixin('flat', {
tasksDone: events.PureEvent('tasksDone'),
// Create/start a task...
//
// Create a task...
// .Task(task)
@ -493,6 +511,26 @@ object.Constructor('TaskManager', Array, events.EventMixin('flat', {
// The ticket is a TaskTicket instance, see it for reference...
//
//
//
// We can also force a specific task to start sync/async regardless
// of the .sync_start setting:
//
// .Task('sync', task)
// .Task('sync', title, task)
// .Task(title, 'sync', task)
// -> task-handler
//
// .Task('async', task)
// .Task('async', title, task)
// .Task(title, 'async', task)
// -> task-handler
//
//
// sync/async start mode apply only to function tasks and tasks that
// have a .start() method like Queue's...
//
//
// NOTE: 'sync' more for a blocking task will block the task manager.
// NOTE: only function tasks accept args.
// NOTE: the task is started as soon as it is accepted.
// NOTE: tasks trigger events only on the task-manager instance that