Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

ui.bootstrap.stackedMap performance improvements? #5925

Closed
deplay opened this issue May 25, 2016 · 1 comment
Closed

ui.bootstrap.stackedMap performance improvements? #5925

deplay opened this issue May 25, 2016 · 1 comment

Comments

@deplay
Copy link
Contributor

deplay commented May 25, 2016

UIBS:1.3.2

Recently,I go through the source code of uibs.
when I come to module 'ui.bootstrap.stackedMap' whose code is down here.

angular.module('ui.bootstrap.stackedMap', [])
/**

  • A helper, internal data structure that acts as a map but also allows getting / removing

  • elements in the LIFO order
    */
    .factory('$$stackedMap', function() {
    return {
    createNew: function() {
    var stack = [];

      return {
        add: function(key, value) {
          stack.push({
            key: key,
            value: value
          });
        },
        get: function(key) {
          for (var i = 0; i < stack.length; i++) {
            if (key === stack[i].key) {
              return stack[i];
            }
          }
        },
        keys: function() {
          var keys = [];
          for (var i = 0; i < stack.length; i++) {
            keys.push(stack[i].key);
          }
          return keys;
        },
        top: function() {
          return stack[stack.length - 1];
        },
        remove: function(key) {
          var idx = -1;
          for (var i = 0; i < stack.length; i++) {
            if (key === stack[i].key) {
              idx = i;
              break;
            }
          }
          return stack.splice(idx, 1)[0];
        },
        removeTop: function() {
          return stack.splice(stack.length - 1, 1)[0];
        },
        length: function() {
          return stack.length;
        }
      };
    }
    

    };
    });

I am wondering why the function 'removeTop' not return stack.pop()
I think 'return stack.pop()' and 'return stack.splice(stack.length - 1, 1)[0];' is equivalent but the former is faster(time ratio is about 1:10)

did I miss something?

@deplay deplay changed the title ui.bootstrap.stackedMap performance improments? ui.bootstrap.stackedMap performance improvements? May 25, 2016
@wesleycho
Copy link
Contributor

I think whoever wrote it probably wrote the remove implementation first - feel free to file a PR changing that.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants