# Performance of map directive

**URL:** <https://www.ruby-forum.com/t/performance-of-map-directive/211420>\
**Category:** NGINX\
**Created:** [September 3, 2011, 5:41pm UTC](https://www.ruby-forum.com/t/performance-of-map-directive/211420 "2011-09-03T17:41:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [September 3, 2011, 5:41pm UTC](https://www.ruby-forum.com/t/performance-of-map-directive/211420/1 "2011-09-03T17:41:04Z")

</div>

Hi,

I understood that if you have only strings in a map directive the access  
time is O(1).  
What if you have the hostnames directive enabled and some of the items  
are  
in the format \*.example.com?  
What about if you mix with regular expressions?

Thanks,  
Calin

---

<div class="post-metadata">

**Author:** ![Calin\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3be4f8/32.png) [@Calin\_D](https://www.ruby-forum.com/u/Calin_D)\
**Post date:** [September 3, 2011, 5:50pm UTC](https://www.ruby-forum.com/t/performance-of-map-directive/211420/2 "2011-09-03T17:50:56Z")

</div>

On Sat, Sep 03, 2011 at 05:11:07PM +0300, Calin D. wrote:

> Hi,
> 
> I understood that if you have only strings in a map directive the access  
> time is O(1).

Yes.

> What if you have the hostnames directive enabled and some of the items are  
> in the format \*.example.com?

nginx tests address by its parts, first “com”, then “example”.  
So access of the operation varies from O(1) to O(N) where N is number of  
parts of longest name. For example, if you test [example.net](http://example.net) only against

\*.example.com  
\*.sub.domain.com

then this will be O(1). If you test [www.sub.domain.com](http://www.sub.domain.com), it will be O(3).

> What about if you mix with regular expressions?

The regular expressions tested sequentially.

nginx tests map in the following order:

1. exact names,
2. \*.names,
3. names.\*,
4. regexes.

The first match stops the tesing.

–  
Igor S.
